[英]Adding Multiple Models to inlineformset_factory
我有一個如下的模型。
class Content(SimpleModel):
title = models.CharField(max_length=255)
body = models.TextField()
slug = models.SlugField(max_length=50)
def __unicode__(self):
return self.title
class MediumStuff(models.Model):
meta_value = models.TextField()
meta_key = models.SlugField('Field Name', max_length=50, blank=True)
content = models.ForeignKey(Content)
def __unicode__(self):
return self.slug
class SmallStuff(models.Model):
text = models.CharField(max_length=60, blank=True, null=True)
content = models.ForeignKey(Content)
我想做的是使用inlineformset_factory()
創建具有用於MediumStuff和SmallStuff模型的內聯表單的內容的formset
我提到了Django文檔 ,但他們有一個如何使用單個外鍵模型的示例。
ContentFormSet = inlineformset_factory(Content, [MediumStuff, SmallStuff])
也不
ContentFormSet = inlineformset_factory(Content, (MediumStuff, SmallStuff))
沒用。
既然可以為管理員添加多個內聯,我相信這可以做到:)
您有任何建議/任何資源或提示嗎? 或者可能告訴我應該在哪里查看管理員如何處理多個內聯?
只需為每個相關模型創建一個內聯:
MediumStuffInline = inlineformset_factory(Content, MediumStuff)
SmallStuffInline = inlineformset_factory(Content, SmallStuff)
看看管理員是怎么做的。 每個內聯由InlineModelAdmin
[1]的子類處理。 內聯本身是在get_formset()
方法[2]上創建的。
查看有關如何在視圖中使用更多一個formset的文檔[3] [4]
[1] http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L228
[2] http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L1243
[3] http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#using-an-inline-formset-in-a-view
[4] http://docs.djangoproject.com/en/1.2/topics/forms/formsets/#using-more-than-one-formset-in-a-view
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.