2个领域独特

时间:2010-05-14 20:54:24

标签: django django-models

我有这个型号:

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)

我希望(用户,电子邮件)是独一无二的。例如:

这是允许的:

  • 1,hello @ hello.com,myblog

  • 2,hello @ hello.com,secondblog

这是不允许的:

  • 1,hello @ hello.com,myblog

  • 1,hello @ hello.com,secondblog

Django有可能吗?

2 个答案:

答案 0 :(得分:7)

有可能,请参阅:模型选项,

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"),)

答案 1 :(得分:2)