On Wed, Nov 29, 2017 at 6:29 AM, Jan Hartman <android4m3@gmail.com> wrote:
Either by specifying PairRecipeCategory or using Categories.recipes.through, I am referencing the model that manages the many-to-many relation. If I wouldn't create an intermediary model myself, Django would do it for me. Both options leave me with the same result - being unable to access the fields from the (virtual) imaginary model.
Indeed, I have tested the example in the documentation (https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#working-with-many-to-many-models ) and it does not work as expected (and produces the same output that you are dealing with in your case). It seems that it produces the inline only for the intermediary model and not linking to the fields of the other model. The example in https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#working-with-many-to-many-models works as expected, seems that this is the case used for the fist one.
I think we have hit a bug, can someone confirm?
--
Dne sreda, 29. november 2017 06.14.19 UTC+1 je oseba Matemática A3K napisala:On Tue, Nov 28, 2017 at 9:12 AM, Jan Hartman <andro...@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 :)To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.--
You received this message because you are subscribed to the Google Groups "Django users" group.com .
To post to this group, send email to django...@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%40goog legroups.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/17349068- .e417-4e14-9daa-762e3a823da3% 40googlegroups.com
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%2BFDnhJ%2BFXhwxxN87Xagjbkcan8jWNMqm%2B7ZjCHf_VTnBkKgKw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment