Django unique_together的子类模型为父属性? [英] Django unique_together on sub-class model for parent attribute?

查看:330
本文介绍了Django unique_together的子类模型为父属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里:

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

    class Meta:
        unique_together = (('account', 'self.user.username'),)

self.user.username 部分显然incorrrect。但是,在这里:

The self.user.username part is obviously incorrrect. However, in this:

class Administrator(User):
    account = models.ForeignKey(Account)

    class Meta:
        unique_together = (('account', 'username'),)

会继续使用User吗? (我不能测试它,因为有太多的元素不在其他地方)。可以使用'user.username'的第一个版本吗?或者,我应该使用第二个版本吗?

would that work since I'm inheriting from User? (I can't test it yet because there are too many elements out of place elsewhere). Can I use the first version with 'user.username' instead though? Or, should I use the second version?

推荐答案

这将是

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


$ b $如果我明白你想做什么,那么b

注意双下划线。这是你如何看外键的对象的属性。

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.

这篇关于Django unique_together的子类模型为父属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆