Django 1.2.1许多字段的内联管理 [英] Django 1.2.1 Inline Admin for Many To Many Fields

查看:111
本文介绍了Django 1.2.1许多字段的内联管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级到Django 1.2.1,因为我特意对具有基本的多对多内联字段。当这样使用管理员时:

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:

初始型号:

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字段中看到的一样的表单,还有一些额外的行。为其提供额外的参数,如成分ModelForm没有帮助。怀疑通过model = Foo.manyfields.through通过基本ModelForm关联可能有问题,我决定看看一个中介模型是否会有所帮助。它现在通过以下方式显示工作内联表单:

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:

新模型:

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)通过中介(或通用管理员)模型进行展示?

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。 (对于详细程度,我很抱歉,这是我的第一篇文章,所以想要彻底)。

TIA. (I apologize for verbosity, it's my first post so wanted to be thorough).

推荐答案

尝试做?

a:

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

这篇关于Django 1.2.1许多字段的内联管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆