Using exclude with annotated FilteredRelation doesn't work.

27 views
Skip to first unread message

Lucas Miller

unread,
Apr 10, 2019, 7:12:13 AM4/10/19
to Django users
Hi, 

when I try to add an exclude filter to an annotated field with a FilteredRelation I get a FieldError on the annotated field. 

For exemple if I modify django test filtered_relation.tests.FilteredRelationTests.test_with_join from  

def test_with_join(self):
self.assertSequenceEqual(
Author.objects.annotate(
book_alice=FilteredRelation('book', condition=Q(book__title__iexact='poem by alice')),
).filter(book_alice__isnull=False),
[self.author1]
)

to 

def test_with_join_exclude(self):
self.assertSequenceEqual(
Author.objects.annotate(
book_alice=FilteredRelation('book', condition=Q(book__title__iexact='poem by alice')),
).exclude(book_alice__isnull=False),
[]
)

I get the following error : django.core.exceptions.FieldError: Cannot resolve keyword 'book_alice' into field. Choices are: book, content_object, content_type, content_type_id, favorite_books, id, name, object_id

I think the issue comme from the function split_exclude(self, filter_expr, can_reuse, names_with_path)  because it create a new Query without the _filtered_relations datas.

How can I manage to do this ?


Reply all
Reply to author
Forward
0 new messages