开发者

Django: Having unique extension numbers associated with a client, but same extension number associated with another client

I'm working on a project where I have models: Client, User, and Extensions, just to make this simple. A User has to be associated with one Client in order to have an Extension number. A User can have extensions, say, 100 and 101. Another User, associated with another Client, can have the same extensions 100 and 101. So, Extensions is not unique in my database so it's allowing a User to have two identical extension numbers when I add it in the Administration, which is wrong. How can check that the extension number to be added is not contained in this user already?

class Extension(models.Model):
    user = models.ForeignKey(User, verbose_name=u"User")
    date_cre开发者_如何学JAVAated = models.DateTimeField(auto_now_add=True, auto_now=True)
    number = models.CharField(max_length=16, unique=False)
    kind = models.SmallIntegerField(choices=KIND_CHOICES,default=KIND_UNKNOWN)

The User class is the default Django class.

class Client(models.Model):
    name = models.CharField(u"Nome", max_length=64)
    last_update = models.DateTimeField(null=True, blank=True)
    last_inbound_call  = models.DateTimeField(null=True, blank=True)
    last_outbound_call = models.DateTimeField(null=True, blank=True)
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=16)


Use Meta.unique_together on user and number in Extension.

class Extension(...):
   ...
  class Meta:
    unique_together = (('user', 'number'),)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜