xRobot
xRobot

Reputation: 26565

2 fields unique

I have this model:

class blog(models.Model):

    user = models.ForeignKey(User)
    mail = models.EmailField(max_length=60, null=False, blank=False)
    name = models.CharField(max_length=60, blank=True, null=True)

I want that (user,email) are unique togheter. For example:

This is allowed:

This is NOT allowed:

Is this possible in Django ?

Upvotes: 0

Views: 180

Answers (2)

miku
miku

Reputation: 188194

It's possible, see: model options,

http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together

class Answer(models.Model):
    user = models. ...
    email = models. ...

    # ...

    class Meta:
        unique_together = (("user", "email"),)

Upvotes: 7

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799520

Meta.unique_together

Upvotes: 2

Related Questions