I think that giving this funcionality names that follow the patterns of
select_related() or prefetch_related() is bad, because although it is very
close to them in subdomains (dealing with relations), its functionality is
much closer to annotate(). In particular, alias is better than to_attr because
the alias here (as in annotate()) can be used in conditions in further filter()
calls, while a prefetched to_attr name cannot.
The one sore point I see in the name 'filtered_relation' is that it's a noun-
phrase, where similar methods are verbs (filter, exclude, annotate, defer) or
verb phrases (select_related). I would prefer a name like
add_filtered_relation, but that may seem cumbersome.
Actually, thinking more about this, a better interface may be
Author.objects.annotate_fitered (
book_alice = ('book', Q(book__title__iexact='poem by alice')),
)
or even
Author.objects.annotate (
book_alice =
FilteredRelation('book', book__title__iexact='poem by
alice')
)
That is, overload annotate to accept, as target, a special object with
initializer
class FilteredRelation(...):
def __init__(relation_name, *q_conditions, **conditions)
My 2 cents,
Shai.
On Saturday 02 September 2017 13:00:08 Adam Johnson wrote:
> I think filter_related is a good name, it's not that confusing that it
> sounds like select_related / prefetch_related. The current name is a bit
> weird because it's an adj + noun, it doesn't really say how it *modifies*
> the QuerySet, it sounds more declarative.
>
> to_attr is also better, as it aligns with prefetch_related.
>
> On 29 August 2017 at 00:32, Josh Smeaton <[email protected]> wrote:
> > There's currently a patch https://github.com/django/django/pull/7560 for
> > ticket https://code.djangoproject.com/ticket/27332 that implements custom
> > join conditions. It was nearly ready for merge for 1.11 but slipped.
> > There are some merge conflicts the author still needs to sort out, but
> > nothing too onerous.
> >
> > Example:
> > Author.objects
> >
> > .filtered_relation(
> >
> > 'book', alias='book_alice',
> > condition=Q(book__title__iexact='poem by alice')
> >
> > )
> >
> > It has received very little attention and I'd like to get it on the radar
> > for folks that would like to see this implemented, and have some time to
> > do review. I'll be trying to get some review done in the next few days
> > depending on my schedule.
> >
> > One minor concern I did have was with the naming of the
> > `filtered_relation` queryset method. I was leaning towards
> > `filter_related` to better align with select_related and
> > prefetch_related but I'm not sure that's better, or if it gives the
> > wrong idea about what the method does. It might also be worth renaming
> > the `alias` kwarg to `to_attr` to align with prefetch_related API. (Even
> > if you don't have time to review, bikeshedding names seems to be popular
> > 'round these parts :) ).
> >
> > --
> > 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 [email protected].
> > To post to this group, send email to [email protected].
> > 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/3f46645a-3535-42e1-9d3c-
> > ef8e5d5d3694%40googlegroups.com
> > <https://groups.google.com/d/msgid/django-developers/3f46645a-3535-42e1-9
> > d3c-ef8e5d5d3694%40googlegroups.com?utm_medium=email&utm_source=footer> .
> > For more options, visit https://groups.google.com/d/optout.