My aim is to access an attribute of a subclass without knowing beforehand which of the two subclasses was choosen (multiple choice classes)
Ideally there is an attribute in the SuperClass that changes depending upon which SubClass was choosen.
The reason is that I have created Forms directly from the SubClasses and use the SuperClass as the entry point for accessing values.
I am aware I can use true or false with hasattr(horse), but ideally I am asking if there is a bit neater solution, such as the SubClass can signal to SuperClass which SubClass was used.
e.g. for product 8 on my list
subclass = getattr(Product(8), 'subclass', 0)
print(subclass)
>> Horse
or
place = Product.location
Print(place)
>> Stable
The whole "problem" stem from the fact that I create Products via SubClass Forms, meanwhile much of the later logic goes top-down, starting with Product
class Product(models.Model):
product_name = models.Charfield(max_length=20)
class Car(Product):
engine = models.Charfield(max_length=20)
location = models.Charfield(default="Garage", max_length=20, editable=False)
product = models.OneToOneField(Product, parent_link=True, on_delete=models.CASCADE)
class Horse(Product):
saddle_model = models.Charfield(max_length=20)
location = models.Charfield(default="Stable", max_length=20, editable=False)
product = models.OneToOneField(Product, parent_link=True, on_delete=models.CASCADE)