On Mon, 2009-05-04 at 03:26 -0700, pbzRPA wrote:
> I would like to know if anyone knows how to create custom operators
> for querysets.
>
> Currently you can do something like:
>
> foo.objects.filter(myfield__icontains = x)
>
> I would like to add my own operator so I can do something like:
>
> foo.objects.filter(myfield__converttext = x)
>
> where "converttext" would be my own operator. Currently there a few
> operators in the django/db/backends/mysql/base.py:
>
> operators = {
> 'exact': '= %s',
> 'iexact': 'LIKE %s',
> 'contains': 'LIKE BINARY %s',
> 'icontains': 'LIKE %s',
> 'regex': 'REGEXP BINARY %s',
> 'iregex': 'REGEXP %s',
> 'gt': '> %s',
> 'gte': '>= %s',
> 'lt': '< %s',
> 'lte': '<= %s',
> 'startswith': 'LIKE BINARY %s',
> 'endswith': 'LIKE BINARY %s',
> 'istartswith': 'LIKE %s',
> 'iendswith': 'LIKE %s',
> }
>
> I would like to add to this dict without modifying the django code.
>
> Any help would be appreciated.
It's possible to add new lookup types, providing you use a custom
queryset, because you have to set the Query.query_terms attribute.
Easiest way to see how this is done in practice, is to look at
django.contrib.gis, which adds a bunch of GIS-specific query terms. In
particular, django/contrib/gis/db/models/sql/query.py.
One day we might add a way to add custom lookup types for fields, but
it's not entirely trivial, as there's a fine balance between making
something that's not too difficult to use, whilst being portable and
without leaking SQL all over the Field subclasses.
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" 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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---