Django unique_together on sub-class model for parent attribute?

后端 未结 3 2173
青春惊慌失措
青春惊慌失措 2021-02-19 10:43

In this:

class Administrator(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    account = models.ForeignKey(Account)

    class Meta:
            


        
3条回答
  •  南旧
    南旧 (楼主)
    2021-02-19 10:48

    It would be

    unique_together = (('account', 'user__username'),)
    

    if I understand what you're trying to do. Note the double underscore. That's how you look at a foreign key's object's properties.

提交回复
热议问题