How to combine multiple querysets and remove duplicates?

85 views
Skip to first unread message

Rodney Lewis

unread,
Jan 9, 2014, 2:29:11 AM1/9/14
to django...@googlegroups.com

Erik Cederstrand

unread,
Jan 9, 2014, 7:18:52 AM1/9/14
to Django Users
Den 09/01/2014 kl. 08.29 skrev Rodney Lewis <pyro...@gmail.com>:

> Please help with my question I posted to StackOverflow:
>
> http://stackoverflow.com/questions/21013973/how-to-combine-multiple-querysets-and-remove-duplicates

If the answer you got there doesn’t solve your problem, then please add a comment on SO describing why.

Erik

Jirka Vejrazka

unread,
Jan 9, 2014, 7:25:15 AM1/9/14
to Django users
Hi Lewis,

  looks like you already got the answer - using Q() objects helps a lot. I'll just add two things that might help:

  - it's possible to prepare filters in advance which helps readability and code structure. I often do something similar to this:

from django.db.models import Q
filters = []
# search for tags
filters.append(tag='sometag')
# handle item title
filters.append(title__icontains='someword')
# only list active users
filters.append(is_active=True)

found = Resources.objects.filter(*filters)

  - you might also want to check out for existing solutions if your case allows. I've recently switched most of my search functionality to django-watson and would not go back. There are other solutions avaliable of course, varying in complexity and capabilities.

  Cheers

     Jirka



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0D4BD660-FF97-44C2-9541-19CE66636D1B%40cederstrand.dk.

Reply all
Reply to author
Forward
0 new messages