'How to query on OneToOne relation of a related_name object in django

I have this model:

class ProgramRequirement(Model):
    program = OneToOneField(Program, related_name='program_requirement')
    prereq_program = ForeignKey(Program, related_name='prereq_program_requirement')
    is_english_required = BooleanField()

and this model

class Program(Model):
    field_1 = ...
    field_3 = ...

I need to write a query that would return the primary key of the programs of which is_english_required of the prereq_program is True.

I tried this but it seems to be a wrong query:

ProgramRequirement.objects.filter(prereq_program__prereq_program_requirement__is_english_required =True).values_list('program__pk', flat=True)

However, it is not returning the correct result.

I am not sure if it is what I want but I am also thinking of this:

Program.objects.filter(prereq_program_requirement__is_english_required =True).values_lis('pk', flat=True)

Any idea of how to retrieve do the abovementioned result?



Solution 1:[1]

You might try:

ProgramRequirement.objects.filter(prereq_program__programrequirement__is_english_required = True).values('pk')

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 RiveN