Django 1.2.1 多对多字段的内联管理
我最近升级到 Django 1.2.1,因为我对 具有基本的多对多内联字段。当像这样使用管理时:
初始模型:
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 字段上看到的表单相同,但有一些额外的行。为它提供额外的参数(例如 Ingredient ModelForm)并没有帮助。我怀疑通过 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)
显然这不是我想使用的黑客。任何人都知道一种通过内联表单显示多对多关系的方法,而无需(a)创建全新的 BasicInline 表单和模板或(b)将其通过中间(或通用管理)模型?
TIA。 (我为冗长道歉,这是我的第一篇文章,所以想要彻底)。
I recently upgraded to Django 1.2.1 because I was specifically interested in the ability to have basic many-to-many inline fields. When using the admin like so:
Initial models:
class Ingredient(models.Model):
name = models.TextField()
class Recipe(models.Model):
ingredients = models.ManyToManyField(Ingredient)
Initial admin:
class IngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeOptions(admin.ModelAdmin):
inlines = [IngredientInline,]
exclude = ('ingredients',)
admin.site.register(Recipe,RecipeOptions)
What I got was the same form you would normally see on a ManyToMany field, with some extra rows. Supplying it with extra parameters like an Ingredient ModelForm did not help. Suspecting that something might be wrong with the basic ModelForm associations via model = Foo.manyfields.through, I decided to see if an intermediary model would help. It now displays a working inline form via:
New models:
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)
New admin:
class IngredientInline(admin.TabularInline):
model = Recipe.ingredients.through
class RecipeOptions(admin.ModelAdmin):
inlines = [IngredientInline,]
admin.site.register(Recipe,RecipeOptions)
Obviously this is not a hack I'd like to use. Anyone know of a way to get a manytomany relationship to display via inline form without either (a) creating an entirely new BasicInline form and template or (b) putting it through an intermediary (or generic admin) model?
TIA. (I apologize for verbosity, it's my first post so wanted to be thorough).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些示例之一是否实现了您想要做的事情?
甲:
乙:
Do one of these examples accomplish what you are trying to do?
a:
b:
如果我没记错的话(自从我完成这部分以来已经有一段时间了),您需要添加 Ingredient 的管理员并将其设置为具有自定义 ModelForm。然后该表单将在 Ingredient 的内联版本中使用。
If I remember correctly (and it's been a while since I've done this part), you need to add the admin for Ingredient and set it to have the custom ModelForm. Then that form will be used in the inline version of Ingredient.