Django: 2 QuerySets and Duplicates

I have two QuerySets both containing the instances of the same model class.

class DBV:
    name = CharField
    description = TextField
    review_state = CharField(choices=[u"Draft",u"Published",u"Archived"])
    team_members = FK(User)
    deleted = Boolean

This is how I am filtering to get the two QuerySets:

res = DBV.objects.filter(deleted=False).filter(team_members=user)
if user.has_perm('dbv.can_view_dbv'):
    r = DBV.objects.filter(deleted=False).filter(review_state__in=[u'Published',u'Archived',])
    res = res + r

The first problem, of course, is that when you try to add QuerySets you get:

unsupported operand type(s) for +: 'QuerySet' and 'QuerySet'

So, what is the best way to merge these QuerySets and remove duplicates? I figure that there is isn't really a way to do that in Django besides writing the sql. Or?

Thank you! :) Eric

Answers


You can convert the queries to lists and add the lists, if you need both sets of results, or you can use "Q" objects and or them together to combine query logic: http://docs.djangoproject.com/en/1.2/topics/db/queries/#complex-lookups-with-q-objects


Need Your Help

CSS sidebar with fixed width and fluid content

html css width fixed fluid

I'm trying to add a sidebar with a fixed width. But the content div should be fluid.

Json compress table?

c# jquery asp.net-mvc json vb.net

In my MVC project, I return like 300 rows, which has exact same structure (fields), so instead of this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.