Skip to content
Advertisement

How to join linked models with foreign key?

I’m coming from Java and new to python / django. So may be my question is silly …

I have one class Parent which contains an association with a class Child.

JavaScript

Parent refers to its ‘Childs’ through the related name ‘children’. At Run time it works well, but at design time, it isn’t reachable from the declaration of the class Parent.

If I want the parent to get a summarized value of its children, the best option I found is to first get the set of children and then to summarize from it

JavaScript

Do you know if there is a better way to do this? Can’t we imagine something like:

JavaScript

In other words: is it possible to directly access the children from the Parent (at design time) without making a query?

Advertisement

Answer

You can always treat the same way this two:

JavaScript

But first you have to have some Parent object. Inside class’ methods of Parent it’s self, otherwise it’s something like:

JavaScript

So basically this should work:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement