I have a django app which basically is just a photo album. Right now I have two models: Image
and Album
. Among other things, each Album
has a foreign key to an Image
to be its thumbnail and each Image
has a foreign key to the Album
it belongs in. However, when I try to use manage.py syncdb
or manage.py sqlall
I get errors saying the class not defined first in models.py isn't defined when it is used in the first class defined.
models.py (abridged):
from django.db import models
import os
class Album(models.Model):
thumb = models.ForeignKey(Image, null=True, blank=True)
class Image(models.Model):
image = models.ImageField(upload_to='t_pics/images')
thumb = models.ImageField(upload_to='t_pics/images/thumbs')
album = models.ForeignKey(Album)
Error I get when I do manage.py sqlall appname
:
[...]
File "/path/to/file/appname/models.py", line 4, in ?
class Album(models.Model):
File "/path/to/file/appname/models.py", line 5, in Album
thumb = models.ForeignKey(Image, null=True, blank=True)
NameError: name 'Image' is not defined
I get the same error when I switch the order of the classes in models.py except it says 'Album' undefined
instead of 'Image' undefined
I also tried commenting the dependancy in the first class then uncommenting after everything else was successfully imported but that didn't help. How should I go about making this work? I'm reluctant to make an entire third class Thumb
because it will have a lot of the same code as Image
I'm also pretty sure I could manually add the foreign key to the database but I want this to be clean and not hackish.