Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django OneToOne reverse access

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

like image 836
WindowsMaker Avatar asked Jun 18 '12 18:06

WindowsMaker


1 Answers

Define a related_name to call the reverse accessor.

a = models.OneToOneField(A, related_name='foobar') # ... a.foobar  
like image 144
Yuji 'Tomita' Tomita Avatar answered Sep 23 '22 05:09

Yuji 'Tomita' Tomita