python - Django Many to Many circular reference? -
conceptually want this:
class child: guardians = models.manytomanyfield(guardian) class guardian: children = models.manytomanyfield(child) the application school. 1 parent(guardian) can have multiple children , child can have multiple guardians. now, can't forward declare in python in c++.
what cleanest , best way this? need third 'relationship' class represent these connections (this i'm tending towards)? before reinvent wheel wanted ask. seems should easy...
i not sure if understand problem.
by doing this:
class child: guardians = models.manytomanyfield('guardian', related_name='children') class guardian: .... other fields # children = models.manytomanyfield(child) <--- not needed is saying "a child can have many guardians , guardian can have many children". don't have declare in both models.
also third(intermediate) table created anyway django, behind scenes. because how model manytomany relationships in rdbms.
the reason you'd want explicitly create intermediate model, when have put information describes specific many2many relationship. i.e.
class child: guardians = models.manytomanyfield('guardian', through='childguardianmembership', related_name='children') class guardian: .... other fields class childguardianmembership: child = models.foreignkey(child) guardian = models.foreignkey(guardian) created_at = models.datetimefield(auto_now_add=true) # when relationship established? in case have aware since declared explicit intermediate model, model use when creating relationship between guardian , child.
e.g.
childguardianmembership.objects.create(child=child_inst, guardian=guardian_inst) adding fields on many2many relationships(as above) described here
Comments
Post a Comment