Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many to Many relation which has either not been installed or is abstract

Consider the following (simplified) Django Models:

class productFamily(models.Model):
    name = models.CharField(max_length = 256)
    text = models.TextField(blank = False)
    image = models.ImageField(upload_to="products/img/")
    def __unicode__(self):
        return self.name

class productModel(models.Model):
    productFamily = models.ForeignKey('productFamily')
    productFamily.help_text = 'ProductFamily to which this model belongs.'
    artNumber = models.CharField(max_length=100)
    name = models.CharField(max_length = 256)
    productDownloads = models.ManyToManyField('productModelDownLoad')
    productDownloads.help_text = 'Files associated to this product Model.'
    def __unicode__(self):
        return self.name

class productModelDownload(models.Model):
    file = models.FileField(upload_to="products/downloads/")
    def __unicode__(self):
        return str(self.file)

I get the following error:

products.productmodel: 'productDownloads' has an m2m relation with model productModelDownLoad, which has either not been installed or is abstract.

I found a page in the django docs that seems to address this, but i can't quite make sense of what it means: http://www.djangoproject.com/documentation/models/invalid_models/

The Model looks valid to me, so what is the problem here?

like image 274
Emanuel Ey Avatar asked Jan 01 '11 18:01

Emanuel Ey


3 Answers

You have to place the class productModelDownload before the productModel class. They are processed from top to down while validating the models.

like image 57
Thomas Kremmel Avatar answered Nov 14 '22 17:11

Thomas Kremmel


models.ManyToManyField('productModelDownLoad') - 'Load' is uppercased

class productModelDownload(models.Model): - 'load' is in lower case

like image 2
Mikhail Korobov Avatar answered Nov 14 '22 16:11

Mikhail Korobov


Interestingly there are two ways to solve this:
a) Thomas's answer does the trick,
b) But, so does Mike Korobov's:
There is a stray capital letter in the field name in the relation:

productDownloads = models.ManyToManyField('productModelDown*L*oad')

Correcting this stray capital also resolves this issue.

like image 1
Emanuel Ey Avatar answered Nov 14 '22 18:11

Emanuel Ey