0

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:

  • 1, hello@hello.com, myblog

  • 2, hello@hello.com, secondblog

This is NOT allowed:

  • 1, hello@hello.com, myblog

  • 1, hello@hello.com, secondblog

Is this possible in Django ?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
xRobot
  • 25,579
  • 69
  • 184
  • 304

2 Answers2

7

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"),)
miku
  • 181,842
  • 47
  • 306
  • 310
2

Meta.unique_together

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358