我有以下模型,其中引用了一个对象和一个它继承自的对象:
class Employee(models.Model):
user = models.OneToOneField(User, blank=True, null=True)
entity = models.ForeignKey('companies.Entity', blank=True, null=True)
brand = models.OneToOneField('companies.Brand', related_name='brand', blank=True, null=True)
class Entity(models.Model):
name = models.CharField('Name', max_length=255, db_index=True)
class Brand(Entity):
company_name = models.CharField(max_length=128, blank=True, null=True)
问题是当我尝试引用反向关系时,我无法仅访问品牌实体。我想让员工与品牌相关联。我试过这个:
brands = Brand.objects.filter(pk=2)
for b in brands:
print b.employee_set.all().query
它输出:
SELECT * FROM `employee` WHERE `employee`.`entity_id` = 2
我希望它输出:
SELECT * FROM `employee` WHERE `employee`.`brand_id` = 2