Hi Gary,

On Mon, 2006-05-22 at 21:20 -0700, Gary Wilson wrote:
> I have a model similar to:
> ===================
> class User:
>     first_name = CharField()
>     last_name  = CharField()
> 
>    def get_full_name(self):
>         return "%s %s" % (self.first_name, self.last_name)
> ===================
> Is there a way I can search by full name (without storing the
> calculated full name in the database)?
> Something like...
> User.objects.filter(get_full_name__iequals="Gary Wilson")

You cannot do this directly as in your example, because the parts inside
filter() get mapped directly to SQL. The name of the fields inside a
filter() are looked up and mapped to their tablename.columnname value
and inserted into the query. Django has no way of knowing how to map
something like the result of get_full_name() to the equivalent database
fields.

So you can either break this query up into its constituent pieces
(User.objects.filter(first_name__iequals = "Gary", last_name__iequals =
"Wilson")) or write a convenience method in the model's manager so that
you can call something like User.objects.filter_by_name("Gary Wilson")
and have it return QuerySet -- to which you can then apply other
filters, etc.

If you aren't familiar with custom managers, have a look at
http://www.djangoproject.com/documentation/model_api/#custom-managers
(or maybe start with the section just before the "Custom Managers"
section in that document).

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

Reply via email to