When you use the defer(...)
query command, Django returns a different class than your original model. How can you dynamically get the model's name when using the defer field?
In code:
obj_nodefer = model_class.objects.filter(title="foo")[0]
model_name = str(type(obj_nodefer)) # Works just fine
obj_defer = model_class.objects.filter(title="foo").defer("content")[0]
model_name = str(type(obj_defer)) # Does't return the right name because of defer above.
How do I get the model's name from obj_defer
?
On the deferred class, you can also get the original class without manipulating the __class__.__name__
string using:
obj._meta.concrete_model
'product.models.ProductModelName'
obj._meta.concrete_model._meta.app_label
'product'
obj._meta.concrete_model._meta.model_name
'productmodelname'
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