Can't use instance_of in 'limit_choices_to' of ForeignKey

129 views
Skip to first unread message

Daniel Abel

unread,
Aug 16, 2010, 5:26:17 AM8/16/10
to django-polymorphic
Hi!

Thanks for django-polymorphic, it is really useful, and much better
than my hand-coded variant I used before.

I noticed, that I can't seem to use a Q object which has instance_of
specified as the limit_choices_to arg of ForeignKey model field.
According to django docs (http://docs.djangoproject.com/en/1.2/ref/
models/fields/#foreignkey) one can use a Q object there, and django-
polymorphic's tests have an example for a "Q(instance_of=Model2B)",
i.e. instance_of appears to work in Q objects. Unfortunately when I
try to use these two together, i.e.:

class MyModel(models.Model):
somekey = ForeignKey(Model2A,
limit_choices_to=Q(instance_of=Model2B))

I get:
django.core.exceptions.FieldError: Cannot resolve keyword
'instance_of' into field. Choices are: [....list of field names ....]

Since I wanted to do this so that ModelForms generated from this model
will limit the choices accordingly, a workaround that works for me is
to override the model field in the ModelForm, as specify the filtering
with a queryset there.

Daniel Abel
Reply all
Reply to author
Forward
0 new messages