(Using Django 1.11)
This question has become confusing, because the reported behavior is not the modern behavior for a related field. Example here, where JobTemplate
is the model class, and credentials
is a many-to-many related field:
>>> JobTemplate._meta.get_field('credentials').__class__
django.db.models.fields.related.ManyToManyField
Is it different if we inspect the _meta
of an object?
>>> JobTemplate.objects.first()._meta.get_field('credentials').__class__
django.db.models.fields.related.ManyToManyField
no.
So here's when I insert what I think is the most likely scenario of someone coming here. You have this:
>>> JobTemplate.objects.first().credentials
<django.db.models.fields.related_descriptors.ManyRelatedManager at 0x6f9b390>
Note, this is what the OP had.
I will stipulate that the related model is Credential
. I can check if this is a related credential manager!
>>> isinstance(JobTemplate.objects.first().credentials, Credential.objects.__class__)
True
ManyToMany fields can be very hard to process, because the attribute kills itself and replaces itself with the manager sub-class. You could also cross-reference this information with the field obtained from get_field('credentials')
to be extra sure. The above isinstance
check could also erroneously pick up other managers that you have set on the model. However, this is still a valuable test to see if the attribute you have "quacks" how a ManyToMany field of that particular related model should.