django OneToOne reverse access

倖福魔咒の 提交于 2019-12-31 11:02:07

问题


I have these simple classes

Class A(models.Model):
    ...

Class Meta(models.Model):
    a = models.OnetoOneField(A, primary_key=True)
    width = models.IntegerField(default=100)

but when I do

a = A()
meta = Meta()
a.save()
meta.a = a
meta.save()
print a.meta.width

i get

'A' object has no attribute 'meta'

Why is this? Am I using OneToOne wrong? if so how can i get the correct print statement?

Thanks


回答1:


Define a related_name to call the reverse accessor.

a = models.OneToOneField(A, related_name='foobar')
# ...
a.foobar 


来源:https://stackoverflow.com/questions/11088901/django-onetoone-reverse-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!