So I wrote this one.
class LimitChoicesToTagsForModel(object):
"""
For use with 'limit_choices_to' parameter in Foreign Relation model fields
"""
def __init__(self, model):
self.ctype_pk = ContentType.objects.get_for_model(model).pk
def add_to_query(self, query, aliases):
# Q objects are stupid complex, instead of duplicating their
behavior just use them directly
query.add_q(models.Q(items__content_type__pk=self.ctype_pk))
query.distinct = True
#Usage:
class Foo(models.Model):
bar_by_tag = models.ManyToManyField(Tag,
limit_choices_to=LimitChoicesToTagsForModel(Bar))
FooAdmin(admin.ModelAdmin):
filter_horizontal =('bar_by_tag',)
Then in admin interface you get nice selection widget with only valid tags
as choices
Original issue reported on code.google.com by njhar...@gmail.com on 31 Dec 2008 at 4:15
Original issue reported on code.google.com by
njhar...@gmail.com
on 31 Dec 2008 at 4:15