class EmailActivaion(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
我能做以下代码吗:
qs=User.objects.filter(email='bla@bla.com')
obj=qs.first()
em = obj.EmailActivaion_set.all()
因为我得到了以下错误:
AttributeError: 'User' object has no attribute 'EmailActivaion_set'
由于您使用
OneToOneField
,这意味着目标模型(这里User
)最多有一个对象附加到它。你知道吗因此,反向关系不是
somemodel_set
,而是简单的somemodel
。你知道吗所以你应该写:
这将直接获取与
User
实例obj
相关的对象。你知道吗你知道吗
你知道吗
你知道吗
相关问题 更多 >
编程相关推荐