[Fixed]-Complex Django model relationships

1๐Ÿ‘

โœ…

I highly recommend checking out the Django documentation on models which can be found here: Django Models

Moreover, to make a symmetrical Many to Many relationship, use:

class B(models.Model):
    bs = models.ManyToManyField("self")

Additionally, I recommend making the relationship between A and B a many to many relationship instead of a foreign key. This will allow you to assign the B to many Aโ€™s while still allowing 1 A to have many Bโ€™s. The same logic should potentially be taken for B and C.

To answer your question about making Bโ€™s required for A, I do not think this is possible. Check out this question for more information: Django 1.7: how to make ManyToManyField required?

๐Ÿ‘คWritten

Leave a comment