Reputation: 558
Are there any caveats involved in using object.__class__
? for example in Django once I did this:
model_object.__class__.objects.all()
I would like to know if that's perfectly fine.
Thanks.
Upvotes: 1
Views: 60
Reputation: 6371
Explicit is better than implicit. Just import the model class::
from .models import ModelClass
ModelClass.objects.all()
Upvotes: 2
Reputation: 678
Not necessarily. <class instance>.__class__
is a reasonable way to get class attributes or methods of an instance. There's usually some better built-in way to interact with dunder attributes and methods though-- In your case you could use type()
to access .__class__
instead, e.g. type(model_object).objects.all()
.
Upvotes: 0