On May 4, 9:53 am, danfis <danfi...@googlemail.com> wrote:
> Hi guys,
>
> i ran into a problem today which i'm not sure if it's a bug (or a
> feature?!) so you might can help me:
>
> when i call a model manager method to manipulate the queryset and
> using Q objects for filtering, it seems like the query of the method
> (=queryset.query) "stores" the result of subqueries and not the
> subquery itself.
> this causes, of course, into some 'old' data sometimes, so i wondered
> if this is correct?
>
> a simple example:
> this is the manager for my model Bar and there is also some other
> model having a foreign key to this one
>
> def get_my_stuff(self):
>   return self.filter(Q(field_1='foobar') | Q(pk__in=[foo.bar_id for
> foo in SomeOtherModel.objects.all()])
>
> the problem is now, that the result of the subquery ([foo.bar_id for
> foo in SomeOtherModel.objects.all()]) is stored as fixed value (and
> not as subquery) in the query and changes in SomeOtherModel objects
> are not recognized correctly.
>
> Any advises?
>

This is expected, because you're explicitly evaluating the result of
SomeOtherModel.objects.all(), thanks to your list comprehension. Using
values_list is probably better, as that produces a lazy
ValuesListQuerySet:

    Q(pk__in=SomeOtherModel.values_list('bar_id', flat=True))
--
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