开发者

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

开发者 https://www.devze.com 2023-02-28 21:18 出处:网络
class ExternalFriends(models.Model): external_user = models.ForeignKey(User) name = models.CharField(max_length=20, null=False, blank=False, db_index=True)
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

精彩评论

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