[Answer]-Django – get the last object of a model with many objects

1👍

Your title doesn’t quite match your question and your question is missing information. But, is this what you want?

fathers = Fathers.objects.all()
sons = Son.objects.filter(father__in=fathers, age=20)

but, assuming all Sons have Fathers, you could just write this as

sons = Son.objects.filter(age=20)

If you are looking for all sons age 20 from one father:

father = Fathers.objects.get(id=101)
sons = Son.objects.filter(father=father, age=20)

This assumes that you have a different model for Father and Son (which is not a good design decision but it is what I understand from your question).

👤Joe

Leave a comment