#22158: Allow model level custom lookups
-------------------------------------+-------------------------------------
     Reporter:  akaariai             |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (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
-------------------------------------+-------------------------------------
Changes (by charettes):

 * cc: charettes (added)


Comment:

 I like your proposed API, it handles all the cases I can think of.

 It also solves a limitation of custom managers used to filter their
 initial queryset, they can't be combined.

 {{{#!python
 class Person(models.Model):
     ROLE_AUTHOR = 'A'
     ROLE_EDITOR = 'E'
     ROLE_CHOICES = [
         (ROLE_AUTHOR, _('Author')),
         (ROLE_EDITOR, _('Editor')),
     ]
     dob = models.DateField()
     role = models.CharField(max_length=1, choices=ROLE_CHOICES)

     @models.lookup
     def author(cls):
         return Q(role=self.ROLE_AUTHOR)

     @models.lookup
     def born_90s(cls):
         return Q(dob__year__gte=1990, dob__year__lt=2000)

 Person.objects.filter(author=True, born_90s=True)
 }}}

 Would you mind exposing this approach to the @developpers mailing list? I
 think we could get interesting feedback there.

--
Ticket URL: <https://code.djangoproject.com/ticket/22158#comment:4>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.eceee5806f879b127fea92a205d507a3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to