Tuesday, November 28, 2017

Re: Admin many-to-many inlines - show editable fields from parent models



On Tue, Nov 28, 2017 at 9:12 AM, Jan Hartman <android4m3@gmail.com> wrote:
Hi, I'm having trouble with inlining models with a many-to-many relationship through the intermediary model. My models are recipes and categories (a category includes multiple recipes and a recipe can belong to multiple categories).

Models (simplified):
class Recipe(models.Model):
title = models.CharField(_('Title'), max_length=100, unique=True)
categories = models.ManyToManyField('Category', through='PairRecipeCategory', verbose_name=_('Categories'))

class Category(models.Model):
title = models.CharField(_('Title'), max_length=100, unique=True)
recipes = models.ManyToManyField('Recipe', through='PairRecipeCategory', verbose_name=_('Recipes'))

class PairRecipeCategory(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)



Admin:

class RecipeInline(admin.TabularInline):
model = PairRecipeCategory
# fields = ['recipe__title']


class CategoryInline(admin.TabularInline):
model = PairRecipeCategory
# fields = ['category__title']

+ ordinary recipe and category ModelAdmin classes

What I want to do is to show editable fields from the parent model (Recipe or Category) in the inlined PairRecipeCategory model instead of just listing the inlines. I get the "Unknown field for model xyz error" because the fields are not reachable from the intermediary model if I uncomment the fields lines.
I've read about this on StackOverflow and such and I could not find a working solution. If anyone has any (even hacky) ideas, I would really appreciate the help.


I think that you are creating an inline for the intermediary model. not an inline for the other model, here is how you should:

And your code is for doing this:

HTH :)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c094f569-4cea-42cd-bd12-05f50e40c8f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BFDnhLqViCqcJpnFNiuztqfFzGW0fDhBmWmS-wWiEWTe%3DmHAQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment