On Thu, 2007-04-19 at 03:11 -0700, MerMer wrote:
> Malcolm,  Must be pilot error  and I can't replicate the error any
> longer - I do apologize for wasting your time.
> 
> However, one additional question.. is there an easy way to a record
> from a queery set dynamically (without touching the DB)?  What I'm
> looking do do in my view is as follows:-
> 
> 1.  Create query set using a filter.
> 2.  Add attributes to query set, via setattr (but not commit these to
> the DB)
> 3.  Dyanmically filter the query set based on these new attributes
> 4.  Return the qs to the template.

Some background information to put my next comment in context:

Because of step 2, what you have at step 3 is no longer a pure QuerySet,
but a cached list of results -- by iterating through them in step 2, you
have pulled all the results out of the database and into the cache. So
further filtering using QuerySet machinery lacks any point. The logic
behind being able to do

        qs1 = MyModel.objects.filter(name='fred')
        qs2 = qs1.filter(date=datetime.now())

is that until you actually access the results of a QuerySet, no database
query is made. So, in the above example, qs1 never results in a call to
the database. In you sequence of steps above, there is no saving to be
made here, because step 2 accesses the members of the queryset.

> Step 3 is still a slight puzzle.  As a work around I am doing the
> following (but I suspect there must be a better way!)
> 
> ls=[]
> for i in qs:
>     if i.newattribute == somevalue:
>           ls.append(i)

That seems logical. You could subclass QuerySet and add your filter as a
method in the subclass if you wanted to. But it's basically the same
code. The built-in queryset filter methods won't be able to help you,
because they are all geared towards querying database table columns.

If memory usage was an issue (and I wouldn't panic too much here -- this
is only relevant if you've got thousands of results, at a guess), you
could create an iterator (using itertools.ifilter, from the Python
standard library, for example) that returned the next result from the
queryset satisfying the constraint and pass the iterator to your
template. That way, the results are only stored once in memory.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
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