开发者

In Django, how do I make uniques on 3 fields?

class ExternalFriends(models.Model):
    external_user = models.ForeignKey(User)
    name = models.CharField(max_length=20, null=False, blank=False, db_index=True)
    external_account_id = models.CharField(max_length=200, null=True, blank=True, db_index=T
rue)

So let's say开发者_开发技巧 I want all 3 fields to be "unique_together". How can I do that?


Use the Meta option unique_together. This takes a tuple (or tuples) describing sets of columns that should be considered unique together.

The model would look like this:

class ExternalFriends(models.Model):
    # columns

    class Meta:
        unique_together = ('external_user', 'name', 'external_account_id'),
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜