unique_together does not work in django 2.0

45 views
Skip to first unread message

FernandoJMM

unread,
Jan 18, 2018, 7:28:35 PM1/18/18
to Django users
Hello,
I have the following class:

==================================
class Silo(models.Model):
    nave = models.ForeignKey(Nave, on_delete=models.CASCADE)
    codSil = models.CharField(
        'Código Silo', db_index=True, max_length=2, default='01')
    notas = models.TextField(null=True, blank=True)

    def __str__(self):
        return "%s %s" % (self.nave, self.codSil)

        class Meta:
            order_with_respect_to = 'nave'
            unique_together = ('nave', 'codSil')
====================================

But the option unique_together does not work because duplicate values ​​exist in the SQLite database for nave and codSil.

To solve it, I have to create the index nave + codSil by hand in the SQLite database ???

Thank you

Jason

unread,
Jan 19, 2018, 6:53:28 AM1/19/18
to Django users
To be clear, you started with an empty database and the duplicate values exist after adding data?

FernandoJMM

unread,
Jan 19, 2018, 12:33:32 PM1/19/18
to Django users

Hello, Problem solved.
For it to work I have to put the META class between the silo class and the method. Thank you

James Bennett

unread,
Jan 19, 2018, 12:39:21 PM1/19/18
to django...@googlegroups.com
You've indented the 'Meta' declaration too much, and Python thinks it's part of the '__str__()' method of your class.

Un-indent it one level.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/555b1903-b6f5-4af1-b7a2-a7921d1fdbac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages