Python 我想通过继承的类访问关系
请参见下面的人为示例。如果我有一个车主,我想接触他们所有的汽车或自行车,我想不出一个干净的方法来做到这一点。请注意,我无法将Vehicle设置为抽象类,因为VehicleIncident类正在FK关系中使用它Python 我想通过继承的类访问关系,python,django,Python,Django,请参见下面的人为示例。如果我有一个车主,我想接触他们所有的汽车或自行车,我想不出一个干净的方法来做到这一点。请注意,我无法将Vehicle设置为抽象类,因为VehicleIncident类正在FK关系中使用它 from django.db import models from foo import Owner class Vehicle(models.Model): owner = models.ForeignKey(Owner) class VehicleIncident(mo
from django.db import models
from foo import Owner
class Vehicle(models.Model):
owner = models.ForeignKey(Owner)
class VehicleIncident(models.Model):
vehicle = models.ForeignKey(Vehicle)
class Car(Vehicle):
steering_wheel = models.CarPartField()
class Bicycle(Vehicle):
handlebars = models.HandlebarField()
如果我认识车主用户,如何获得特定的车辆类型
这会引发一个错误:
对象没有属性
Bicycle.objects.filterowner=用户有用吗?@DanielRoseman:嗯,我觉得我在寻找一种不同的方法来对待用户,但你的建议完全有效。由于车辆带有车主的外键,并且不是抽象的,车主将设置车辆而不是自行车。你必须使用丹尼尔建议的方法。
owner.bicycle_set.all()