On Jul 9, 10:59 am, Alex <alexle...@googlemail.com> wrote:
> Hi,
> I am getting a 'QuerySet' object has no attribute 'reel_set' on a
> reverse relationship query.
>
> I am pretty sure the models are correct which has made me look at what
> else might be the problem. So far I have thought of:
>
> - no relationships instances have been created between the two models
> so effectively the set is empty which is a problem - is it?  (this
> seems like an unlikely cause)
>
> - the 'Reel' model was created *after* instances of its foreign key
> had been created - so I am wondering if syncdb doesn't create the _set
> field - this also seems an unlikely cause of the problem as adding
> foreign keys to models with existing instances is likely to happen a
> lot during development
>
> - something to do with INSTALLED_APPS ? I have changed the order to
> ensure that the foreign key model (Reel)  appears before the model it
> has a foreign key to.
>
> - other ideas?
>
> Thanks for any thoughts,
>
>                                             Alex

Although you haven't posted your models, or the actual code causing
the problem, the reference to a QuerySet gives a hint as to the
problem.

Foreign key relationships are of course attributes of individual model
instances, not querysets. So this won't work:

    MyObject.objects.filter(whatever=whatever).reel_set.all()    # NO

because filter returns a queryset, not an instance. You could do:

    MyObject.objects.get(whatever=whatever).reel_set.all()

because get returns a single instance, assuming there is only one that
matches the query parameters (otherwise you will get a
MultipleObjectsReturned exception). Or, you could do:

   MyObject.objects.filter(whatever=whatever)[0].reel_set.all()

where you get an individual object from the qs before accessing its
reel_set relation (although if there are no matches, you will get an
IndexError).
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to