Well, it didn't seem like there was a simple way to do this, so I delved into django.contrib a bit and built an app for handling this: Django Merged Inlines. If it would be useful to anyone, it's available here:
Django admin - Mixing multiple model inlines in single admin interface
-
07-07-2023 - |
سؤال
I have a Django model layout that looks something like this:
class Author(models.Model):
name = models.CharField(max_length=50, unique=True)
class Publication(models.Model):
author = models.ForeignKey(Author)
#Some shared fields
class Meta:
abstract = True
class Book(Publication):
#Book specific fields
class Poem(Publication):
#Poem specific fields
Generally speaking, I'd like to show information listed by author and sorted by some information, like pub_date, so that the end user would see something like:
Author A:
- Poem 1
- Poem 2
- Book 1
- Poem 3
- Book 2
Author B:
- Book 1
- Book 2
- Poem 1
- Poem 2
- Book 3
And so on. I can figure out how to display these on the front-end (I can either use django-polymorphic or simply combine the author.book_set.all() and author.poem_set.all() into a single list and sort). But I CANNOT figure out how to implement this in the Admin site. I would like the structure to be exactly the same as above -- so that when I click on Author A I'd get:
- inline for Poem 1
- inline for Poem 2
- inline for Book 1
- inline for Poem 3
- inline for Book 2
If I simply attach Poem and Book as TabularInlines, they would be separated out into separate fieldsets, like so:
- inline for Poem 1
- inline for Poem 2
inline for Poem 3
inline for Book 1
- inline for Book 2
But I think it's important for functionality that they are mixed together in a single fieldset (with the fields specific to the other child class either grayed out or not present for each inline). Does anyone have any idea how to implement this?
المحلول