我有以下模型类似于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)
我试图从这个品牌中获得这样的员工:
attendees = Brand.objects.filter(pk=2)
for a in attendees:
print a.employee
我收到错误:
'品牌'对象没有属性'员工'
我也尝试过:
attendees = Brand.objects.filter(pk=2)
for a in attendees:
print a.employee_set
并获得:
'品牌'对象没有属性'employee_set'
如何从employee
获取brand
?
由于
答案 0 :(得分:0)
问题是相关名称:
brand = models.OneToOneField('companies.Brand', related_name='brand')
我把它改为:
brand = models.OneToOneField('companies.Brand', related_name='employee')