For an example of how the admin app does this see search_fields
http://www.djangoproject.com/documentation/model_api/#search-fields
On 5/23/06, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
>
> 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
>
>
>
> >
>
--
----
Waylan Limberg
[EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---