Ramblings

April 24, 2008

Concat Django Querysets

Filed under: dev, django, orm, tip — michaelangela @ 9:46 pm

Here is a good discussion on concatenating Django Querysets. Very helpful! It also introduced me to itertools. This leads to some other talk about itertools. Very interesting!

The jist though is like this:

How do you concatenate two querysets? – Django users | Google Groups

import itertools
q1 = Model1.objects.all()
q2 = Model2.objects.all()
for thing in itertools.chain(q1,q2):
do_something(thing)

Very handy.

About these ads

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 43 other followers

%d bloggers like this: