代码之家  ›  专栏  ›  技术社区  ›  xRobot

独特的2个领域

  •  0
  • xRobot  · 技术社区  · 15 年前

    我有这个模型:

    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,secondblog

    这是不允许的:

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

    • 1,hello@hello.com,secondblog

    这在Django是可能的吗?

    2 回复  |  直到 15 年前
        1
  •  7
  •   miku    15 年前

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

    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
  •  2
  •   Ignacio Vazquez-Abrams    15 年前