For couple days I am trying to do multiple inline models in django. (inline inside inline)
But I couldnt. Then I decided to define a formset while editing base model (Page in example below), So one can also add new link when adding a new page(in admin page).
Like page->linksection->link. Here is the model.
#model.py
class Page(models.Model):
title = models.CharField(max_length=255)
class LinkSection(models.Model):
page = models.ForeignKey(Page)
title = models.CharField(max_length=255)
class Link(models.Model):
linksection = models.ForeignKey(LinkSection)
text = models.CharField(max_length=255)
url = models.URLField()
#admin.py
class LinkSectionInline(admin.TabularInline):
form = LinkSectionForm + LinkForm
Can you please show me how to define a formset for this model?
Thank you very much.
Related
I have a Book Model and BookImages model. Book is not related to Images directly ,instead BookImages has Book as foreign key and a Book can have many images.
I want to show Book details along with images related to book in admin panel but here Book doesnt have any relation to BookImages. Instead BookImages points to Book Model.
How can i show admin view with Book details and all Book Images.
Below is exact model code.
class Book(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length = 5000)
class BookImage(models.Model):
book = models.ForeignKey('Book', on_delete=models.CASCADE)
image = models.ImageField(upload_to=settings.MEDIA_RELATIVE_ROOT + "/bookimages")
Python version is 3.7.12 and django version is 3.2.14
Use TabularInline or StackedInline admin classes:
Example:
# admin.py
class BookImageInline(admin.TabularInline):
model = BookImage
fields = ['image']
extra = 1
class BookAdmin(admin.ModelAdmin):
inlines = [BookImageInline]
I have two models:
Python 3.7
class ClassOne(models.Model):
name = models.CharField(max_length=10, default='')
class ClassTwo(models.Model):
name = models.CharField(max_length=20, default='')
class_ones = models.ManyToManyField(ClassOne)
Now I would like to show ClassOne in django-admin with all ClassTwo's listed. I already tried to create a Tabular (admin.TabularInline) or create a method in ClassOne as following:
def get_class_twos(self):
return self.classtwo_set.all()
and include that method in the fieldsets, but that did not work either. Neither did directly putting classtwo_set.all() or classtwo_set in the fieldset list.
You first should define TabularInline:
class ClassOneInLine(admin.TabularInline):
model = ClassTwo.classone.through
form = ClassOneForm
Then add it to admin class for ClassTwo:
class AuthorAdmin(admin.ModelAdmin):
inlines = (ClassOneInLine,)
And don't forget to register admin.
TL;DR: I need a some kind of formset for formsets.
I have two different models related to one buisness-entity, and I need to make a form to edit both models like a one form. And I need to create a lot of such forms on the one page like Django inline formset does.
Now I have the following thing:
class Parent(models.Model):
name = models.Charfield()
class FirstChild(models.Model):
name = models.Charfield()
e_id = models.IntegerField()
parent = models.ForeignKey(Parent)
class FirstChildForm(django.forms.ModelForm):
class Meta:
model = Child
fields = ('name', 'e_id', 'parent')
widgets = {'parent': forms.TextInput}
And I render a lot of them using inline formsets:
formset_class = inlineformset_factory(Parent, FirstChild,
form=FirstChildForm, extra=1)
But now I have to add second child model and a form for it, and still render it like an one inline form, but make it form actually edit two models. Like this:
class SecondChild(models.Model):
name = models.Charfield()
e_id = models.IntegerField()
parent = models.ForeignKey(Parent)
class SecondChildForm(django.forms.ModelForm):
class Meta:
model = Child
fields = ('name', 'e_id', 'parent')
widgets = {'parent': forms.TextInput}
formset_class = inlineformset_factory(models=[Parent, FirstChild],
forms=[FirstChildForm, SecondChildForm],
extra=1)
As far as I understand, Django formsets cannot work with multiple models right now.
So which way should I choose to implement this behaviour and do not broke all django conceptions?, I cannot use some extra libraries so I have to implement everything by myself and I use django 1.6 if it is important.
So, finally I used this approach as a base: https://micropyramid.com/blog/how-to-use-nested-formsets-in-django/
I have a model Teacher which has following data:
class Teacher(models.Model):
name = models.CharField(max_length=100)
thumbnail = models.ForeignKey(Thumbnail)
class Thumbnail(models.Model):
thumbnail = models.FileField(upload_to=upload_to)
class TeacherAdmin(admin.ModelAdmin):
list_display = ('name',)
Here I want to relate all the thumbnail in my project to Thumbnail field. In my admin it shows me a dropdown of thumbnail while I want to add the thumbnail from admin.
Is it possible that I can upload from admin too?
Also what can I do in the case like :
class Teacher(models.Model):
name = models.CharField(max_length=100)
thumbnail = models.ManyToManyField(Thumbnail)
If I want to add multiple Images.
The only issue I am having is on admin side. Client side can be handles from regular forms.
Need suggestions and Ideas
my models.py
class PagePlaceholder(models.Model):
content_type = models.ForeignKey()
object_id = models.PositiveIntegerField()
display_content = generic.GenericForeignKey('content_type', 'object_id')
class Page(models.Model):
page_name = models.CharField(max_length=1000,default="sample_page")
placeholder = models.ManyToManyField('PagePlaceholder')
the placeholder can refer to any of 4 classes by the generic foreign key relationship.
page has a many2many relationship to placeholder to say that a page can contains a bunch of different models.
and my admin.py for the same.
class PagePlaceholderAdmin(admin.ModelAdmin):
pass
class PageInline(GenericStackedInline):
model = PagePlaceholder
class PageAdmin(admin.ModelAdmin):
inlines = [PageInline,]
now what i want is in the page admin view, to show the form for editing the pageplaceholder's display content object. trying to get that inline gives me an error saying that object is not a class.
is there a decent solution for this with the admin.py or is it necessary for me to create a new form and view to display these model forms properly?