Django Admin Changing Inline Admin Form based on class type
I asked some questions about Django inheritance in a previous question. Now I am trying to figure out how to get the admin interface to work with it.
If I had some models like this:
class ContentItem(models.Model):
title = models.CharField(max_length=1000)
page_order = models.IntegerField()
last_update_date = models.DateTimeField(default=datetime.now())
class Meta:
abstract = True
ordering = ['page_order', 'last_update_date', 'title']
class LinkContent(ContentItem):
url = models.URLField()
link_text = models.CharField(max_lenth=1000)
class TextContent(ContentItem):
text = models.CharField()
class VideoContent(ContentItem):
title = models.CharField()
video_file = models.FieldField(upload_to = 'videos')
class Page(models.Model):
contents = models.ManyToManyField(ContentItem)
title = models.CharField()
Assume I am using django-model-utils as was mentioned in the previous question's answer.
How would I make the admin interface show the correct inline based on the child class? I want the correct inline to show for the type. So if the I have 3 items in contents, some text, a link and a video. Then when I look at that Page in the admin interface, I want to see the inline form for each item.
How is this accomplished?
Also, how would I handle adding new things to the relation? Ideally, when the user wants to add a new ContentItem they would be asked what kind of content item they need to add. Then the add form for that content type would open in a pop up. So if I specify I want to add VideoContent, then I get a pop up that lets me upload the video. If I specify I want TextContent I get a pop up where I enter the text.
It seems like special consideration is needed for the change form for the Page model. Any suggestions on how to handle this issue?
So it turns out that if you use django-model-utils and do the inheritance. Then you can add inlines for each of the sub classes. The Django admin will figure it out. It will show the right form for the class.
Problem solved.
链接地址: http://www.djcxy.com/p/65420.html