【发布时间】:2011-03-10 08:14:00
【问题描述】:
我最近升级到 Django 1.2.1,因为我对 have basic many-to-many inline fields 的功能特别感兴趣。像这样使用管理员时:
初始模型:
class Ingredient(models.Model):
name = models.TextField()
class Recipe(models.Model):
ingredients = models.ManyToManyField(Ingredient)
初始管理员:
class IngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeOptions(admin.ModelAdmin):
inlines = [IngredientInline,]
exclude = ('ingredients',)
admin.site.register(Recipe,RecipeOptions)
我得到的是与您通常在 ManyToMany 字段中看到的相同的表单,但有一些额外的行。为它提供额外的参数(如成分模型表单)并没有帮助。怀疑通过 model = Foo.manyfields.through 的基本 ModelForm 关联可能有问题,我决定看看中间模型是否有帮助。它现在通过以下方式显示一个有效的内联表单:
新型号:
class RecipeJoin(models.Model):
pass
class Recipe(models.Model):
ingredients = models.ManyToManyField(RecipeJoin,through='Ingredient')
class Ingredient(models.Model):
name = models.TextField()
test = models.ForeignKey(RecipeJoin,null=True,blank=True,editable=False)
新管理员:
class IngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeOptions(admin.ModelAdmin):
inlines = [IngredientInline,]
admin.site.register(Recipe,RecipeOptions)
显然这不是我想使用的 hack。任何人都知道一种通过内联表单显示多对多关系的方法,而无需 (a) 创建全新的 BasicInline 表单和模板或 (b) 将其置于中间(或通用管理员)模型中?
TIA。 (我为冗长表示歉意,这是我的第一篇文章,所以想要彻底)。
【问题讨论】:
标签: django django-admin django-forms