Reputation: 29775
I have the following model similiar to the relationship in the django docs https://docs.djangoproject.com/en/1.11/ref/models/relations/:
class Employee(models.Model):
user = models.OneToOneField(User, blank=True, null=True)
company = models.ForeignKey('companies.Company', related_name='company', blank=True, null=True)
brand = models.OneToOneField('companies.Brand', related_name='brand', blank=True, null=True)
I try to get the employee from the Brand like this:
attendees = Brand.objects.filter(pk=2)
for a in attendees:
print a.employee
I get the error:
'Brand' object has no attribute 'employee'
I have also tried:
attendees = Brand.objects.filter(pk=2)
for a in attendees:
print a.employee_set
and get:
'Brand' object has no attribute 'employee_set'
How can I get the employee
from the brand
?
Thanks
Upvotes: 1
Views: 367
Reputation: 29775
The problem was related name:
brand = models.OneToOneField('companies.Brand', related_name='brand')
I changed it to:
brand = models.OneToOneField('companies.Brand', related_name='employee')
Upvotes: 1