using F expression to access a field from one to many relationship not using the queryset resulted from prefetch_related method

Viewed 22

hey guys let's say I have these models

class Object1:
    .....
class Object2:
    user = models.ForeignKey(User)
    rel = models.ForeignKey(Object1, related_name = 'objects')
    isCOmpleted = models.BooleanField()

and I wanted to perform this query:

Object1.objects.all().prefetch_related(Prefetch('objects', Object2.objects.filter(user = specific_user))).annotate(is_completed=F('objects__isCompleted'))

and the user is only related to one object from the Object2, but I got duplicate objects in my query, and I know the reason is that I have two objects from Object2 in my database for two different users, but the problem is that F expression didn't look in my prefetched query using prefetch_related method, I tried the exact same thing in the shell and it's giving me the results that I have expected but not in the view, so what's the problem here exactly any help would be really appreciated

0 Answers
Related