Rachel Willmer wrote:
> I have a complicated query which should return the subset of a table
> whose objects match one of several conditions.
> 
> What's the easiest way of doing this, given that I can't just do a SQL
> statement, because some of the conditions require me to feed the
> object's values into a function to evaluate it.
> 
> I've had a few ideas:
> 
> a) Pulling back the entire table, and deleting from the query set
> those objects which don't match.
> 
> Is this possible? I get the feeling that using delete() would delete
> the entry from the db, which i don't want to do.

You shouldn't actually delete anything from a full list of records. Just 
filter it in a new list (and then Python promptly discards unneeded 
records from memory):

     records = [r for r in Model.objects.all() if r.check_condition()]

(this assumes that check_condition returns something sensibly evaluating 
to True or False)

> I can't find any documentation to show how to union 2 querysets - is
> it possible?

Since you anyway will get them in memory entirely you can just create 
actual lists from querysets and then concatenate them (and may be check 
for duplicates if needed):

     result = list(queryset1) + list(queryset2)

But your option a) anyway looks more reasonable.

> c) Writing a custom Manager.

Managers don't do anything magical, you still have to choose wither a) 
or b) way. Whether to place this code into a Manager or just let it lay 
somewhere in the code is the whole different question, it's the matter 
of convenience.

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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