I have three models:
class User:
screen_name = Charfield
class Post:
author = FK(User)
class Comment:
post = FK(Post, related_name=comment_set)
author = FK(User)
Now I want to annotate Posts in following way (original annotation is more complicated, added simpler example):
if is_student:
comment_qs = Comment.objects.annotate(
comment_author_screen_name_seen_by_user=Case(
When(Q(is_anonymous=True) & ~Q(author__id=user.id), then=Value("")),
default=F("author__screen_name"), output_field=CharField()
),
comment_author_email_seen_by_user=Case(
When(Q(is_anonymous=True) & ~Q(author__id=user.id), then=Value("")),
default=F("author__email"), output_field=CharField()
),
)
queryset = queryset.annotate(
post_author_screen_name_seen_by_user=Case(
When(Q(is_anonymous=True) & ~Q(author__id=user.id), then=Value("")),
default=F("author__screen_name"), output_field=CharField()
),
post_author_email_seen_by_user=Case(
When(Q(is_anonymous=True) & ~Q(author__id=user.id), then=Value("")),
default=F("author__email"), output_field=CharField()
),
)
else:
comment_qs = Comment.objects.annotate(
comment_author_screen_name_seen_by_user=F("author__screen_name"),
comment_author_email_seen_by_user=F("author__email")
)
queryset = queryset.annotate(
post_author_screen_name_seen_by_user=F("author__screen_name"),
post_author_email_seen_by_user=F("author__email"),
)
queryset = queryset.prefetch_related(Prefetch("comment_set", queryset=comment_qs))
After this I want to filter Posts by comment_set__comment_author_screen_name_seen_by_user field, but I receive following error:
django.core.exceptions.FieldError: Unsupported lookup 'comment_author_screen_name_seen_by_user' for AutoField or join on the field not permitted
But this field can be accessed:
queryset[0].comment_set.all()[0].comment_author_screen_name_seen_by_user == "Foo Bar"
I thing something wrong with Prefetch, but can't tell what exactly. Any thoughts?