Someone has again brought up the lack of custom join support in django - 
specifically LEFT OUTER joins: https://code.djangoproject.com/ticket/29262

I figure it's probably something that we'd like the ORM to support but I 
don't think we've ever really landed on a design that's palatable. I'd like 
to try and get a rough consensus for a design and flesh out any likely 
issues. We can then either provide a ready to go syntax that someone could 
implement, or I could put together a DEP. At a minimum, we can point future 
askers towards a design and "PR Welcome" them. Best case someone (possibly 
myself) runs with the design and provides an implementation.

What I'm interested in seeing are specific use cases for customising joins 
as that will drive any kind of design.

I've been playing around with some syntaxes, and this is the kind of thing 
I'm currently leaning toward:

qs = Book.objects.annotate(
my_reviews=joins.Left(
Review.objects.filter(user=u1),
book=OuterRef('pk')
)
)

This supports customising both the join type, and the join conditions (ON 
book.id = review.book_id). One concern with this design is that now the 
result set contains multivalues - where a book might be repeated if the 
number of reviews that match the filter is greater than 1. We could ask the 
user to specify whether the result is potentially multi-valued and convert 
the result into a list, but that would require ordering and comparing the 
current row to the previous row to check if it's a multivalue. These are 
the kind of issues I want to flesh out, and that's only going to be 
possible given a sufficient corpus of examples.

So I'll kick off with an example that seems to be rather common:

*- Add filtered results from a reverse relation / m2m relation to the 
result set without eliminating results using a LEFT JOIN.*

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/3c3ce7f4-4138-4a80-b43a-3613e8d65070%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to