Last active
October 5, 2016 09:29
-
-
Save singingwolfboy/ac8a8b6c04729b797f61e97916102a7d to your computer and use it in GitHub Desktop.
Well, since you offered to help...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Parent(models.Model): | |
name = models.CharField() | |
class Category(models.Model): | |
"Each Parent object has its own set of categories" | |
parent = models.ForeignKey(Parent, related_name="categories") | |
name = models.CharField() | |
class Item(models.Model): | |
""" | |
Each Parent object has its own set of items, | |
which can be assigned to categories, | |
but only categories that are also assigned to the same Parent. | |
""" | |
parent = models.ForeignKey(Parent, related_name="items") | |
category = models.ForeignKey(Category, related_name="items") | |
name = models.CharField() | |
# The problem is that autogenerated form fields, | |
# including the Django admin pages and Wagtail (https://wagtail.io) | |
# include *all* possible categories in the Item.category queryset, | |
# instead of *only* those categories scoped to the item's parent. | |
# I thought I might be able to do something like this: | |
# | |
# category = models.ForeignKey(Category, related_name="items", | |
# limit_choices_to=models.Q(category__parent=models.F('category')) | |
# ) | |
# | |
# But that doesn't appear to work either, since `limit_choices_to` | |
# seems to operate from the perspective of Category, not Item. Help? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'll need to override the ModelForm's
__init__
method, and change the queryset there.