I have two models that are related. From the object of one, I want to get the class name of the other. Currently, I'm doing it this way:
associated_model = getattr(object_specific, associated_model_str)
associated_model_instance = associated_model.all().first()
if associated_model_instance:
associated_model_name = associated_model_instance.__class__.__name__
In this case, object_specific
is an object of a model. associated_model_str
is the name of the attribute in that model that has a reference to the second model. When I get the attribute into associated_model
, this variable contain the RelatedManager for the second model. If I do .all().first()
I will get an object from that model, from where I can get the class name by checking the attribute .__class__.__name__
. The problem is that sometimes I don't have any instances of the second model in the database associated to the instance of the first model. In other words, the .all()
comes empty. So I don't have an instance of the second class to get the class name. How can I get that class name directly from the RelatedManager?