SELECT name,birthdate FROM friends
UNION
SELECT name,birthdate FROM enemies
ORDER BY birthdate, name;
I can't find any reference in the Django docs to getting a UNION.
Thanks,
Michael
several alternatives:
Use a custom manager:
####################################################
class FriendManager(Manager):
def __init__(self, is_friend):
self.is_friend = is_friend
def get_query_set(self):
return super(FriendManager, self).get_query_set(
).filter(is_friend=self.is_friend)
class People(Model):
name = CharField(...)
birthdate = DateField(...)
is_friend = BooleanField(...)
friends = FriendManager(True)
enemies = FriendManager(False)
objects = Manager()
...
friends = People.friends.all()
enemies = People.enemies.all()
everybody = People.objects.all()
young_friends = People.friends.filter(
birthdate__gt=date(1990,1,1))
old_enemies = People.enemies.filter(
birthdate__lt=date(1943,1,1))
####################################################
Or, you could just use itertools.chain:
for person in itertools.chain(
Friends.objects.all(),
Enemies.objects.all()):
print person.name, "(%s)" % person.birthdate
-tim
Thanks. Unfortunately doing it that way returns a list of tuples - a bit
hard to work with.
Is there some way to get a "normal" (i.e. dictionary-like) response
without having to resort to making my own connection with psycopg2?
Thanks,
Michael