When running a migration, I am currently getting this lazy reference ValueError whenever the model that I have a defined ManyToManyField is in a separate file from the model that I am targeting. However, when I place the two models in the same file, I am able to successfully run the mirgation.
- Why is this error happening?
- Is it possible to separate the models (community.py and community_member.py) into separate files?
ValueError: contains a lazy reference to fitness.communitymember, but app 'fitness' doesn't provide model 'communitymember'.
My directory structure looks like this:
my_app
|----fitness
|----user.py
|----community.py
|----community_member.py
community.py
class Community(models.Model):
id = models.AutoField(
primary_key=True,
)
owner = models.ForeignKey(
User,
)
members = models.ManyToManyField(
User,
through='CommunityMember',
through_fields=('community', 'member')
)
class Meta:
db_table = 'Communities'
community_member.py
class CommunityMember(models.Model):
community = models.ForeignKey(
Community,
db_column='community_id'
)
member = models.ForeignKey(
User,
db_column='member_id',
)
class Meta:
db_table = 'Community_Members'
settings.py
INSTALLED_APPS = [
'my_app.fitness'
]
If I place the contents of community_members.py inside of community.py the migration is successfully executes, but I am not sure why.