【问题标题】:Many to Many relation which has either not been installed or is abstract未安装或抽象的多对多关系
【发布时间】:2011-06-02 06:47:35
【问题描述】:

考虑以下(简化的)Django 模型:

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)

我收到以下错误:

products.productmodel: 'productDownloads' 与模型 productModelDownLoad 具有 m2m 关系,该模型要么尚未安装,要么是抽象的。

我在 django 文档中找到了一个似乎解决这个问题的页面,但我不太明白它的含义: http://www.djangoproject.com/documentation/models/invalid_models/

模型在我看来是有效的,那么这里有什么问题?

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    您必须将类 productModelDownload 放在 productModel 类之前。 它们在验证模型时从上到下进行处理。

    【讨论】:

      【解决方案2】:

      models.ManyToManyField('productModelDownLoad') - 'Load' 是大写的

      class productModelDownload(models.Model): - 'load' 是小写的

      【讨论】:

      • 谢谢,我也注意到了。
      【解决方案3】:

      有趣的是,有两种方法可以解决这个问题:
      a) 托马斯的回答可以解决问题,
      b) 但是,Mike Korobov 的也是如此:
      关系中的字段名称中有一个杂散的大写字母:

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

      纠正这个流浪的资本也解决了这个问题。

      【讨论】:

      • 有趣的是,通过改变模型的顺序,它们也得到了验证。这甚至是需要的吗?
      猜你喜欢
      • 2014-01-29
      • 2018-06-15
      • 2012-01-03
      • 1970-01-01
      • 2021-04-01
      • 2016-06-24
      • 2016-08-27
      • 2016-11-20
      • 2021-08-26
      相关资源
      最近更新 更多