#27332: Specifying additional ON arguments, and more flexibility with joins
-------------------------------------+-------------------------------------
     Reporter:  MikiSoft             |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:
  (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 Nicolas Delaby):

 Hi,
 I started to look at this one and realized {{{GenericForeignKey}}}
 implemented such functionnality in {{{ get_extra_restriction() }}}

 
https://github.com/django/django/blob/master/django/contrib/contenttypes/fields.py#L437

 So I followed that path, and came up with a PoC implementation (with
 passing tests), following this API:

 {{{#!python
 class ConditionalJoinTests(TestCase):

     @classmethod
     def setUpTestData(cls):
         cls.author1 = Author.objects.create(name='Alice')
         cls.author2 = Author.objects.create(name='Jane')

         cls.book1 = Book.objects.create(title='Poem by Alice',
                                         editor='A',
                                         author=cls.author1)

         cls.book2 = Book.objects.create(title='The book by Jane A',
                                         editor='A',
                                         author=cls.author2)

         cls.book2 = Book.objects.create(title='The book by Jane B',
                                         editor='B',
                                         author=cls.author2)

     def test_conditional_join_query_wo_join(self):
         """
         All Authors are returned because no join is required by the
 filters.
         """
         self.assertQuerysetEqual(
             Author.objects
             .conditional_join('book', title__iexact='poem by alice'),
             ["<Author: Alice>", "<Author: Jane>"])

     def test_conditional_join_query_with_join(self):
         self.assertQuerysetEqual(
             Author.objects
             .conditional_join('book', title__iexact='poem by alice')
             .filter(book__isnull=False),
             ["<Author: Alice>"])

         self.assertQuerysetEqual(
             Author.objects
             .conditional_join('book', Q(title__iexact='poem by alice'))
             .filter(book__isnull=False),
             ["<Author: Alice>"])

     def test_conditional_join_query_with_join_multiple(self):
         self.assertQuerysetEqual(
             Author.objects
             .conditional_join('book', title__icontains='jane', editor='B')
             .filter(book__isnull=False),
             ["<Author: Jane>"])
 }}}

 If this proposal receives some interest, I'll be glad to work on preparing
 a pull request to support this use case.

--
Ticket URL: <https://code.djangoproject.com/ticket/27332#comment:10>
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.895d1de93e260fd575766d6eae9188f4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to