Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 380
amann Avatar asked May 17 '10 12:05

amann


1 Answers

You should be able to check it as a string.

field.__class__.__name__ == 'ManyRelatedManager'
like image 133
Brant Avatar answered Sep 28 '22 23:09

Brant