【问题标题】:Django 1.2.1 Inline Admin for Many To Many Fields多对多字段的 Django 1.2.1 内联管理
【发布时间】: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


    【解决方案1】:

    如果我没记错的话(自从我完成这部分以来已经有一段时间了),您需要为成分添加管理员并将其设置为具有自定义 ModelForm。然后,该表单将在成分的内联版本中使用。

    【讨论】:

    • 我也是这么想的,但我试了一下,得到了相同的下拉列表。很奇怪。
    【解决方案2】:

    这些示例之一是否完成了您想要做的事情?

    一个:

    # Models:
    
    class Ingredient(models.Model):
        name = models.CharField(max_length=128)
    
    class Recipe(models.Model):
        name = models.CharField(max_length=128)
        ingredients = models.ManyToManyField(Ingredient, through='RecipeIngredient')
    
    class RecipeIngredient(models.Model):
        recipe = models.ForeignKey(Recipe)
        ingredient = models.ForeignKey(Ingredient)
        amount = models.CharField(max_length=128)
    
    
    # Admin:
    
    class RecipeIngredientInline(admin.TabularInline):
        model = Recipe.ingredients.through
    
    class RecipeAdmin(admin.ModelAdmin):
        inlines = [RecipeIngredientInline,]
    
    class IngredientAdmin(admin.ModelAdmin):
        pass
    
    admin.site.register(Recipe,RecipeAdmin)
    admin.site.register(Ingredient, IngredientAdmin)
    

    b:

    # Models:
    
    class Recipe(models.Model):
        name = models.CharField(max_length=128)
    
    class Ingredient(models.Model):
        name = models.CharField(max_length=128)
        recipe = models.ForeignKey(Recipe)
    
    
    # Admin:
    
    class IngredientInline(admin.TabularInline):
        model = Ingredient
    
    class RecipeAdmin(admin.ModelAdmin):
        inlines = [IngredientInline,]
    
    admin.site.register(Recipe,RecipeAdmin)
    

    【讨论】:

    • 是的,我最终确实决定使用 ForeignKey,但我实际上是在寻找一种可以与 ManyToMany 字段一起使用的内联表单,因为 django 1.2 文档似乎说......这些工作,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2011-12-24
    • 2014-10-20
    • 2015-02-28
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    相关资源
    最近更新 更多