【问题标题】:2 fields unique2 个字段唯一
【发布时间】:2011-02-19 17:19:54
【问题描述】:

我有这个模型:

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,我的博客

  • 2,hello@hello.com,第二博客

这是不允许的:

  • 1,hello@hello.com,我的博客

  • 1,hello@hello.com,第二博客

这在 Django 中可行吗?

【问题讨论】:

标签: django django-models


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      相关资源
      最近更新 更多