I am trying to get the associated name of a multi-valued field. The m2m field is located between the Group and Treatment models and is defined in the group model as follows:
lections = models.ManyToManyField(Lection, blank=True)
The field is as follows:
<django.db.models.fields.related.ManyToManyField object at 0x012AD690>
Print field.__dict__ :
{'_choices': [], '_m2m_column_cache': 'group_id', '_m2m_name_cache': 'group', '_m2m_reverse_column_cache': 'lection_id', '_m2m_reverse_name_cache': 'lection', '_unique': False, 'attname': 'lections', 'auto_created': False, 'blank': True, 'column': 'lections', 'creation_counter': 71, 'db_column': None, 'db_index': False, 'db_table': None, 'db_tablespace': '', 'default': <class django.db.models.fields.NOT_PROVIDED at 0x00FC8780>, 'editable': True, 'error_messages': {'blank': <django.utils.functional.__proxy__ object at 0x00FC 7B50>, 'invalid_choice': <django.utils.functional.__proxy__ object at 0x00FC7A50>, 'null': <django.utils.functional.__proxy__ object at 0x00FC7 A70>}, 'help_text': <django.utils.functional.__proxy__ object at 0x012AD6F0>, 'm2m_column_name': <function _curried at 0x012A88F0>, 'm2m_db_table': <function _curried at 0x012A8AF0>, 'm2m_field_name': <function _curried at 0x012A8970>, 'm2m_reverse_field_name': <function _curried at 0x012A89B0>, 'm2m_reverse_name': <function _curried at 0x012A8930>, 'max_length': None, 'name': 'lections', 'null': False, 'primary_key': False, 'rel': <django.db.models.fields.related.ManyToManyRel object at 0x012AD6B0>, 'related': <RelatedObject: mymodel:group related to lections>, 'related_query_name': <function _curried at 0x012A8670>, 'serialize': True, 'unique_for_date': None, 'unique_for_month': None, 'unique_for_year': None, 'validators': [], 'verbose_name': 'lections'}
The field should now be accessible through the lection instance. Thus, this is done using lection.group_set
But I need to access it dynamically, so I need to get the related_name attribute somewhere.
Here in the documentation there is a note about the ability to access ManyToManyField.related_name , but this does not work for mine somehow ..
Help would be greatly appreciated. Thanks in advance.
Edit: Is there a need to put a class above all my models that defines the related_name attribute, and therefore each model has a similar name? like here maybe? → docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name