我有一个名为 Vehicle 的 PolymorphicModel 类,我从中派生了两个类:Car 和 Truck。
我使用 UpdateView 进行更新,但它随机从 vehicle_list 获取 vehicle_id。而且我必须控制该型号是哪种类型的车辆。但我做不到。
我的课程:
class Vehicle(PolymorphicModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name="ID")
class Car(Vehicle):
car_license = models.CharField(max_length=32)
class Truck(Vehicle):
truck_license = models.CharField(max_length=32)
鉴于:
class VehicleUpdate(UpdateView):
car = object
if car.car_license is not None:
model = Car
else:
model = Truck
fields = '__all__'
def get_success_url(self, **kwargs):
return reverse_lazy('person-list')
但它给出了这个错误:
AttributeError: type object 'object' has no attribute 'tr_id'
顺便说一句,我尝试使用“isinstance()”,但它没有用。也许是关于成为 PolymorphicModel,我不知道。
class VehicleUpdate(UpdateView):
if isinstance(object, Car):
model = Car
else:
model = Truck
fields = '__all__'
def get_success_url(self, **kwargs):
return reverse_lazy('person-list')