开发者

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 开发者_Python百科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.


You should be able to check it as a string.

field.__class__.__name__ == 'ManyRelatedManager'


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


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


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__
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜