How can I include a ManyToMany field into django admin fieldsets? - django

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.

Related

Display a many-to-many relation in admin without need to edit children

My model is:
class CustomerAccount(models.Model):
name = models.CharField(max_length=50)
class MyUser(AbstractUser):
customer_account = models.ManyToManyField(CustomerAccount, related_name='users', blank=True)
default_customer_account = models.ForeignKey(CustomerAccount, related_name='users_using_default_account', null=True, blank=True)
I want to display in the admin interface of the CustomerAccount this sort of thing:
I don't need to add a MyUser in the CustomerAccount interface.
Most SO questions and docs are related to show an Inline class in the admin, but I don't need it.
How should I do?
The functinality shown above you get by making adding the desired field to the filter_horizontal list (docu).

nested forms django, nested models

I have two classes say A and b in django models
class Address(models.Model):
address_1 = models.CharField(max_length=128)
address_2 = models.CharField(max_length=128,blank=True)
class Facility(models.Model):
name = models.CharField(max_length=250, unique=True)
location = models.ForeignKey(Address)
I want to create django admin form so that I can edit both Facility name and address when I add Facility object in admin interface.
I dont want the reverse, means I dont want to edit Facility name when I add Address object.
Any help will be appreciated.
Do you know about inline admin?
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin

Howto define a formset in django (like multiple inline)

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.

Django - Foreign key reverse creation

Imagine this model:
class ExercisePart(models.Model):
exercise = models.ForeignKey(Exercise)
sequence = models.IntegerField()
class Meta:
unique_together = (('exercise', 'sequence',),)
class Exercise(models.Model):
name = models.CharField(max_length=15)
From the admin interface I'd like to be able to create/link ExerciseParts through the Exercise page. I'd like to do that because I wish to avoid having to go on another page each time I want to add an ExerciseParts.
Is it possible ? How can I do that ?
You're looking for the inline admin feature.
admin.py
class ExercisePartInline(admin.TabularInline):
model = ExercisePart
class ExerciseAdmin(admin.ModelAdmin):
inlines = [ExercisePartInline]

Multi-table inheritance in the admin view

I have the following two models (multi-table inheritance):
class Funnies(models.Model):
title = models.CharField(max_length=200)
class FunniesProfile(Funnies):
body = models.TextField()
I register both using the admin.site.register(). When I want to add a new "Funnies" entry I get the "title" field only. However, I would also like to get the field for the "body" so when an admin creates a new Funnies entry he will have the option to add a FunniesProfile entry that will automatically get the ptr_id of the Funnies entry. How can I do that?
Meir
You can probably use this instead:
models.py:
class Funnies(models.Model):
title = models.CharField(max_length=200)
class FunniesProfile(models.Model):
funnies = models.OneToOneField(Funnies)
body = models.TextField()
admin.py:
class FunniesProfileInline(admin.TabularInline):
model = FunniesProfile
class FunniesAdmin(admin.ModelAdmin):
inlines = [
FunniesProfileInline,
]
site.register(Funnies, FunniesAdmin)