#22158: Allow model level custom lookups
-------------------------------------+-------------------------------------
     Reporter:  Anssi Kääriäinen     |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 With the approach we've taken with `RegisterLookupMixin` this could be
 implemented by

 1. Having `models.Model` extend `RegisterLookupMixin`
 2. Adding a `lookup` decorator as described above that wraps the decorated
 function in something that has a `contribute_to_class` method. This method
 would then call `model.register_lookup` with a lookup factory.
 3. Adjusting `sql.Query.build_lookup` to consider `self.model.get_lookup`
 and call into the wrapped function with `rhs`

 e.g. the object that could be registered as a model lookup would be
 something like

 {{{#!python
 class ModelLookupFactory:
    def __init__(self, method):
        self.model = None
        self.method = method

    def contribute_to_class(self, cls, name, private_only=False):
        self.model = cls
        cls.register_lookup(self, name)

    def __call__(self, rhs):
        # XXX: Use introspection on self.method to avoid passing rhs
        # if the arity of self.method is 1. Ensure isinstance(rhs, bool)
        # and negate if necessary
        return lookups.Exact(self.method(self.model, rhs), True)

 lookup = ModelLookupFactory
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22158#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070182c8b9f136-5fe67546-f3f5-443e-886e-e9af62a85528-000000%40eu-central-1.amazonses.com.

Reply via email to