How do I write a Django model with ManyToMany relationsship with self through a Model -
i want have model manytomany relationship itself, don't know how write i'l try write code illustrate want do.
class person(models.model): name = models.charfield() occupation = models.charfield() friends = models.manytomanyfield('self', through = personfriends) my model want friends go through
class personfriends(models.model) ??? comment = models.charfield() in manytomany field through relationship if other model's name "pet" example i'd name fields in through class person , pet , make them models. foreignkey(person) , pet example
what name fields in personfriends model 2 person-fields same model?
you can this:
class person(models.model): name = models.charfield(max_length = 255) occupation = models.charfield(max_length = 255) friends = models.manytomanyfield('self', through = 'personfriends', symmetrical = false) # ^^^^^^^^^^^ # has false when using `through` models. or else # model not validate. class personfriends(models.model): source = models.foreignkey(person, related_name = 'source') # ^^^^^^^^^^^^ # need different `related_name` each when have # multiple foreign keys same table. target = models.foreignkey(person, related_name = 'target') comment = models.charfield(max_length = 255)
Comments
Post a Comment