开发者

Django 1.2.1 Inline Admin for Many To Many Fields

开发者 https://www.devze.com 2023-01-05 02:56 出处:网络
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:

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


Do one of these examples accomplish what you are trying to do?

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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号