2

Using django version 1.11.2 final.

I have the model below, for small invoices. I made several migrations along the way, and eventually added the unique_together = ('facture', 'article') constraint into the 'Line' model. However, when running ./manage.py makemigrations, no changes are detected. Also tried with unique_together = (('facture', 'article'),) syntax, but get the same result.

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')
Lapin-Blanc
  • 1,945
  • 1
  • 17
  • 26

1 Answers1

2

You have capitalised your meta class incorrectly. It should be

class Line(models.Model):
    ...

    class Meta:
        unique_together = ('facture', 'article')
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Omg, what a stupid mistake... Should I delete my question ? – Lapin-Blanc Jun 09 '17 at 11:07
  • 1
    It's up to you. Questions are often closed when the problem is a simple typo, but if you leave the question then it might help somebody who has a similar problem in future. – Alasdair Jun 09 '17 at 11:09