Hi!

I am new to Django and just discovered an issue when trying to filter
querysets with multi-value relations.

Let's say I have a Article and Tag model where an Article can have
many Tags. I have a use case where a bunch of get parameters filters
the list of articles based on Article model fields similar to this:

articles = Article.objects.all()
article_length = request.GET.get('article_length', '')
if some_condition:
  articles.filter(article_length__gte = article_length)

This type of conditional filter chaining works well. However, it isn't
possible to add a filter to limit the query set based on multi value
relations like this:

# code above plus:

if some_other_condition:
  #apply second filter
  articles.filter(tag__id__in = [1,2,3])

The docs [1] say:

"Successive filter() calls further restrict the set of objects, but
for multi-valued relations, they apply to any object linked to the
primary model, not necessarily those objects that were selected by an
earlier filter() call."

What is the correct way to conditionally filter on multivalue
relations? Do force the loading of the previous filtering and then
manually loop over the collection to remove items?

Regards,

Peter

[1]: 
http://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to