postgresql - Django model reference like loop? -


i want know how django model circular reference... like,

class family(models.model):     fname = models.charfield(max_length=30,unique=true)    member_id = models.foreignkey(member)   class member(models.model):    name = model.charfield(max_length=30,unique=true)    family_id = models.foreignkey(family) 

these 2 familiy , member models. want circular reference...

main goal bind member under each families. that's works fine. query parse family table , want main member of particular family like, want family head each family means shall do?

explain circular reference or better method available that. or shall foreign key both. please solution.

that's not right approach, shouldn't have circular reference in models. if want have relationships that, depend on requirement:

  1. if 1 person can has 1 family, put family foreign key in member model.

  2. if 1 person can have multiple families, put member manytomany field in family model.

also, don't name field member_id or family_id. django model not database design, python class code well. try use member , family instead.

gentle suggestion: please don't put description of question in code block tags. try make wording formal possible(cap first).

edit:

if want member details of family, it's easy in django well:

# model class family(models.model):     fname = models.charfield(max_length=30,unique=true)  class member(models.model):    name = model.charfield(max_length=30,unique=true)    family = models.foreignkey(family)  # family family = family.objects.get(id=1) # family members all_family_members = family.member_set.all() 

django doc following relationship backwards.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -