I have a PolymorphicModel class named Vehicle, and I derived two class from this: Car and Truck.
I use UpdateView to update but it's getting vehicle_id from vehicle_list randomly. And I have to control which type of vehicle is this model. But I couldn't.
My classes:
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)
In view:
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')
But it's giving this error:
AttributeError: type object 'object' has no attribute 'tr_id'
By the way, I tried to use "isinstance()" but it didn't work. Maybe it's about being PolymorphicModel, I don't know.
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')