10

How can you check the type of a many-to-many-field in django?

I wanted to do it this way:

import django  
field.__class__ == django.db.models.fields.related.ManyRelatedManager

This doesn't work, because the class ManyRelatedManager can't be found. But if i do field.__class__ the output is django.db.models.fields.related.ManyRelatedManager

Why does it refer to a class that doesn't seem to exist and how can i bring it to work?

Many thanks for your help.

amann
  • 5,449
  • 4
  • 38
  • 46

4 Answers4

19

You should be able to check it as a string.

field.__class__.__name__ == 'ManyRelatedManager'
Brant
  • 5,721
  • 4
  • 36
  • 39
  • 1
    Thanks for the quick response! But isn't that a little bit undynamic? Actually, i don't think that the class "ManyRelatedManager" will be ever renamed, but isn't it a little bit sloppy? But however, i think I'll use this answer, becuase it is more performant than lazerscience's one. Thanks! – amann May 17 '10 at 13:35
7

If you already have the field instance you can simply do:

if isinstance(field, ManyToManyField):
    pass // stuff

If you only have the related manager instance, you can reverse lookup the field instance:

>>> print fm
<class 'django.db.models.fields.related.ManyRelatedManager'>
>>> print fm.instance._meta.get_field_by_name('fieldnamehere')
(<django.db.models.fields.related.ForeignKey: fieldnamehere>, None, True, False)

This has only been tested on Django 1.5

SleepyCal
  • 5,739
  • 5
  • 33
  • 47
Botond Béres
  • 16,057
  • 2
  • 37
  • 50
  • This would be the perfect way, but i don't know how to get the field-instance of a many-to-many-field, that is located at the target-model.. if you could help me with this, i'll accecpt your answer :) – amann May 17 '10 at 14:17
  • 1
    I can list all the fields with `object._meta.get_all_field_names()` but the corresponding fieldname isn't accessible via `object._meta.get_field('fieldname')`. I can use the function `object._meta.get_field_by_name('fieldname')` which puts out `(, None, False, True)`. Actually i don't know how to get the field-instance out of that.. – amann May 17 '10 at 14:22
  • 1
    got it :).. it is: `object._meta.get_field_by_name("fieldname")[0].field` – amann May 17 '10 at 14:27
2

(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.

AlanSE
  • 2,597
  • 2
  • 29
  • 22
0

I'm not quite sure what you are trying to achieve, but i guess you should better look into your model's _meta attribute for determining the field class! check out _meta.fields and _meta.many_to_many!

You could do something like:

field_class = [f for f in yourmodel._meta.many_to_many if f.name=='yourfield'][0].__class__
Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148