class Machine(models.Model):
name= models.CharField( max_length=120)
class Meta:
abstract = True
class Car(Machine):
speed = models.IntegerField()
class Computer(Machine)
ram = models.IntegerField()
My question is, how can I understand what type is the Machine model. For instamce I know the incoming query is a children of the Machine model but I also want to know it is a Car submodel.
verbose_name is a human-readable name for the field. If the verbose name isn't given, Django will automatically create it using the field's attribute name, converting underscores to spaces. This attribute in general changes the field name in admin interface.
The @property decorator is a built-in decorator in Python for the property() function. This function returns a special descriptor object which allows direct access to getter, setter, and deleter methods. A typical use is to define a managed attribute x : class C(object): def __init__(self): self.
I am not sure if I understand your question correctly. If you are trying to find out the type of a given instance you can use the built-in type
function.
an_object = Car(name = "foo", speed = 80)
an_object.save()
type(an_object) # <class 'project.app.models.Car'>
Or if you wish to check if an_object
is an instance of Car
you can use isinstance
.
isinstance(an_object, Car) # True
isinstance would work only if you fetched the object calling the Car class. if you do Machine.objects.all() and later want to know if is a car, what you can do is use hasattr. like:
o = Machine.objects.all()[0]
print(hasattr(o, 'car'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With