I want to get the class of the sender in the code below using sender.__class__
. This works until I have a model that inherits from a base class.
For example sender is class Profile but sender.__class__
gives the base bass Profile inherits from MPTTModelBase (which is abstract = True
). Same thing with .Meta
etc.
@receiver(post_save)
def send_func(sender, instance, created, **kwargs):
print(sender) #<class 'demo.apps.t.models.Profile'>
print(sender.__class__) # <class 'mptt.models.MPTTModelBase'>
How do I get the class ignoring inherited models (if it has one)?
For most django signals, the sender of the signal is the class itself. Here, you get sender
as the class and the actual object is instance
.
So what you will get is:
sender.__class__
: the metaclass of your model. sender._meta
: the model's Meta options. You get it here because Django removes the Meta
from the model and makes options available as _meta
, which it augments with some methods.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.