django OneToOne reverse access

DatabaseDjangoOne to-One

Database Problem Overview


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

Database Solutions


Solution 1 - Database

Define a related_name to call the reverse accessor.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWindowsMakerView Question on Stackoverflow
Solution 1 - DatabaseYuji 'Tomita' TomitaView Answer on Stackoverflow