On Mon, 2007-10-08 at 12:14 -0700, Dan Goldner wrote:
> Hello,
> Is it possible to use filter() to query objects based on comparisons
> among attributes of the same object? For example, if my model Task has
> two attributes start_year and end_year and I want to select all tasks
> where start_year was somehow set later than end_year, I would try
> something like
> 
>     q_backwards = models.Task.objects.filter(end_year__lt =
> start_year)
> 
> but that gives a NameError ("global name start_year is not defined").
> Is there any way to reference model object properties on the right
> hand side of the filter argument?
> 
> All of the query examples in the docs compare the value of some
> attribute to a constant. If the answer is that you can't compare one
> attribute to another, is there a preferred strategy for this? A model
> method, or custom SQL (hoping to avoid that 'cause I'd have to learn
> SQL) or ....?

At the moment, this requires custom SQL. That is partly why the "where"
parameter to extra() exists.

It will be possible in the future to do a lot of this type of thing more
directly. Russell Keith-Magee had a clever idea a few months back about
how we can make this work with a reasonable syntax -- using something
similar to Q() objects -- and in the near future (certainly before 1.0),
we'll flesh this out and implement it. All the supporting infrastructure
is in place in the queryset-refactor branch and once that's merged we
can fill in the details of his dastardly plan and make it happen.

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 [email protected]
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