Django Admin 根据类类型更改内联管理表单

发布于 2024-10-24 20:01:27 字数 1309 浏览 6 评论 0原文

我在上一个问题中问了一些有关 Django 继承的问题。现在我想弄清楚如何让管理界面与它一起使用。

如果我有一些这样的模型:

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()

假设我正在使用 django-model-utils,如上一个问题的答案中提到的。

我如何使管理界面根据子类显示正确的内联?我希望为该类型显示正确的内联。因此,如果我的内容有 3 个项目,一些文本、一个链接和一个视频。然后,当我在管理界面中查看该页面时,我想查看每个项目的内联表单。

这是如何实现的?

另外,我将如何处理向关系中添加新事物?理想情况下,当用户想要添加新的 ContentItem 时,系统会询问他们需要添加什么类型的内容项。然后该内容类型的添加表单将在弹出窗口中打开。因此,如果我指定要添加VideoContent,那么我会看到一个弹出窗口,让我上传视频。如果我指定我想要TextContent,我会在其中输入文本。

看来Page模型的变更表单需要特别考虑。关于如何处理这个问题有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半寸时光 2024-10-31 20:01:27

所以事实证明,如果你使用 django-model-utils 并进行继承。然后您可以为每个子类添加内联。 Django 管理员会解决的。它将显示适合班级的正确形式。

问题解决了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文