Reputation: 13662
I checking few field names passed by the APIs from the browser and validating them before passing to Django ORM queries.
The question is for a given mode and given field name, how can I determine if the field is declared as Many-to-Many field using the Content-Type framework?
Upvotes: 1
Views: 90
Reputation: 52173
You can get the field and check its .many_to_many
property:
>>> content_type = ContentType.objects.get(model="<model_name>")
>>> field = content_type.model_class()._meta.get_field("<field_name>")
>>> field.many_to_many
True
Upvotes: 1
Reputation: 13662
You might not require content-type for this to be precise. If you already have the model or an instance handy, you would get the list of m2m fields as below
m2m_fields = [field.name for field in _object._meta.many_to_many]
If you are after just the field name a.k.a concrete fields use as below
fields = [field.name for field in _object._meta.concrete_fields ]
Upvotes: 0