2个领域独特

xRo*_*bot 0 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)
Run Code Online (Sandbox Code Playgroud)

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

这是允许的:

  • 1,hello @ hello.com,myblog

  • 2,hello @ hello.com,secondblog

这是不允许的:

  • 1,hello @ hello.com,myblog

  • 1,hello @ hello.com,secondblog

Django有可能吗?

mik*_*iku 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"),)
Run Code Online (Sandbox Code Playgroud)