在添加unique_together约束后,makemigrations没有看到更改

时间:2017-06-09 10:30:36

标签: django django-models django-1.11

使用django 1.11.2版本。

我有下面的模型,适用于小发票。我在此过程中进行了多次迁移,最终将unique_together = ('facture', 'article')约束添加到了' Line'模型。但是,运行./manage.py makemigrations时,未检测到任何更改。也尝试使用unique_together = (('facture', 'article'),)语法,但得到相同的结果。

from django.db import models
from django.db.models import F, Sum
from personnes.models import Person

# Create your models here.
class Facture(models.Model):
    date = models.DateField(auto_now=True)
    client = models.ForeignKey(Person, related_name="factures", on_delete= models.CASCADE)
    articles = models.ManyToManyField("Article", through="Line", related_name="factures")

    @property
    def total(self):
        return self.lines.aggregate(total=Sum(F('count')*F('article__price')))

    def __str__(self):
        return "%s %s %s" % (self.id, self.date, self.client.first_name)

class Article(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return "%s %s" % (self.name, self.price)

class Line(models.Model):
    facture = models.ForeignKey(Facture, related_name="lines", on_delete=models.CASCADE)
    article = models.ForeignKey(Article, related_name="lines", on_delete=models.CASCADE)
    count = models.IntegerField(default=1)

    @property
    def amount(self):
        return self.count * self.article.price

    def __str__(self):
        return "facture %s - %s x %s %s" % (self.facture.id, self.count, self.article.name, self.article.price)

    class META:
        unique_together = ('facture', 'article')

1 个答案:

答案 0 :(得分:2)

您错误地将元类大写了。它应该是

class Line(models.Model):
    ...

    class Meta:
        unique_together = ('facture', 'article')
相关问题