'How avoid duplicate records when use annotate() in queryset

I get this queryset for some method an receive one record

candidates_qs = self.filter_job_type_language_zones(job_type_id, filter_languages, filter_zones)

Then i need to cast a charfield to integerfield to compare against some value

candidates_qs = self.filter_job_type_language_zones(job_type_id, filter_languages, filter_zones)
        candidates_qs = candidates_qs.annotate(
            # cast answer_text as integer to compare with question value
            as_integer=Cast('candidatejobtype__questions_answers__answer_text', IntegerField()))

After that my queryset have duplicated records. I try distinct() but didn't work. How avoid duplicate records?



Solution 1:[1]

What about relation between models? In your annotation, there is a lookup so my assumption is that it could make duplicates.

There is only one object in queryset, but is in relation with many questions_answers and each has its own answer_text.

I think, some models definitions here would be helpful.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1