Reputation: 689
I have some problem with order of defining models in django,so i want some thing like this :
class Album(models.Model):
mainTrack = models.OneToOneField(Track)
class Track(models.Model):
albumID = models.ForeignKey(Album)
and in this way when i want run makemigration command,django give this error:
Track is not defined
there is exist any way to solve that??
Upvotes: 2
Views: 85
Reputation: 5873
You should add related_name="track"
to your ForeignKey
call.
class Album(models.Model):
mainTrack = models.OneToOneField(Track)
class Track(models.Model):
albumID = models.ForeignKey('Album', related_name="track")
Upvotes: 0
Reputation: 6488
As you already noticed this line
mainTrack = models.OneToOneField(Track)
references Track
but obviously Track
is not defined at this time.
Solution:
Reference to the Track
model using a string:
mainTrack = models.OneToOneField('Track')
This is also mentioned in the docs:
If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself.
Upvotes: 2