views:

87

answers:

2

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

A: 

If I remember correctly (and it's been awhile 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.

Kenneth Love
That's what I thought as well, but I tried it and got the same list of drop downs. Very odd.
Katharine
A: 

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)
Damian
Yes, I eventually did decide to settle for a ForeignKey, but I was actually looking for an inline form that would work with a ManyToMany field as the django 1.2 documentation seems to say... Still, both of these work, so thanks!
Katharine