python - Django - get the last object of a model with many objects -


i have 2 models. father , son model. , relationship in son model is:

father = models.foreignkey(father, related_name="father") 

now in real database, have 5 fathers, each father has 5 sons. want find sons age 20.

father = father.objects.all() #how find sons age 20? 

your title doesn't quite match question , question missing information. but, want?

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

but, assuming sons have fathers, write as

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

if looking sons age 20 1 father:

father = fathers.objects.get(id=101) sons = son.objects.filter(father=father, age=20) 

this assumes have different model father , son (which not design decision understand question).


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -