Friday, December 15, 2017

ManyToManyRel without through

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:
https://www.lasolution.be/blog/related-manytomanyfield-django-admin-site.html

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/84c19ed0-c52e-4398-b300-8450fd90922b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment