Wednesday, February 29, 2012

Re: Filtering model searches by a property (as opposed to a field)

On Wed, Feb 29, 2012 at 3:16 PM, Tom <t.scrace@gmail.com> wrote:
> Is it not possible to filter based on a property?

queries are compiled to SQL to be sent and processed at the database;
properties are Python code, the database knows nothing about them.
that's why the compiler only allows you to use database fields on a
filter


> What is the correct method for doing this?

the generic answer is to do it in Python, something like:

result = [ record in queryset if record.property == value ]

but this can be very inefficient, and nowhere as flexible as Django's querysets.

much better is to translate the criteria into something the database
can process. In this case, find the beginning and end of the current
week and use a range filter.


--
Javier

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment