ManyToManyRel without through

31 views
Skip to first unread message

Mirek Zvolsky

unread,
Dec 15, 2017, 4:12:04 PM12/15/17
to Django users
Hi, I'm a beginner in Django.
I try implement both sides acces to m2m relation and I have found this old article about it:

Starting question:
1) is this still the good way in 2017/dj2.0? do we have something better for this goal?

If no, then it almost works for me,
but I cannot add the wrapper for [+] (for adding of new records) - that has been described in the end of the article.

I have this code:
class PersonForm(forms.ModelForm):
...
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
# if this is not a new object, we load related teams
self.initial['teams'] = self.instance.team_set.values_list('pk', flat=True)
rel = ManyToManyRel(self, Team)
self.fields['teams'].widget = RelatedFieldWidgetWrapper(self.fields['teams'].widget, rel, admin.site)

The last 2 lines should add the wrapper, but there is a problem.
I use a ManyToManyField without through=..

ManyToManyRel() creates the object (with default parameter through=None), however it fails while creating RelatedFieldWidgetWrapper with error
   'NoneType' object has no attribute '_meta'
in django 2.0 code django/db/models/fields/reverse_related.py, ManyToManyRel class:
   def get_related_field(self):
          opts
= self.through._meta ...

where self.through is None.

So my main question:
2) Is this issue of the ManyToManyRel class, which allows the default parameter through=None, however fails in its get_related_field() when the default is used,
or, should I change my code...?

Best regards,
Mirek
Reply all
Reply to author
Forward
0 new messages