In my project, I have 2 models with a ManyToMany field that point to each other. In this case, I have Elections and Candidates. The idea is that an Election can have multiple Candidates and that a Candidate can also be part of multiple elections (One 'Candidate' is only one person).
I have the following:
project/elections/models.py
from candidates.models import Candidate
class Election(models.Model):
candidates = models.ManyToManyField(Candidate)
...
project/candidates/models.py
from elections.models import Election
elections = models.ManyToManyField(Election)
...
When I try to run any command (makemigrations, runserver, etc.) I get a circular dependency between Election and Candidate which crashes. I have the models in different apps as a coding practice.
Should I:
Move both models to one app and one file
Not have the models pointing to each other (how would I then accomplish my goal?)
Do something different
You do not need to do this. A many-to-many field is already bidirectional. Just define it on one side, and use the reverse relation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With