Skip to content

fixing "assignment to the reverse side of a many-to-many.."#5

Open
matinmidietsoir wants to merge 1 commit intokux:masterfrom
matinmidietsoir:master
Open

fixing "assignment to the reverse side of a many-to-many.."#5
matinmidietsoir wants to merge 1 commit intokux:masterfrom
matinmidietsoir:master

Conversation

@matinmidietsoir
Copy link
Copy Markdown

in extend.py add_bidirectional_m2m

fixing the bug : "direct assignment to the reverse side of a many-to-many set is prohibited.
I use what I found here https://gist.github.com/Wtower/0b181cc06f816e4feac14e7c0aa2e9d0

sorry if my procedure isn't correct, it's my first pull request :).

        def save(self, commit=True):
            """
            Saves this ``form``'s cleaned_data into model instance
            ``self.instance``.

            If commit=True, then the changes to ``instance`` will be saved to the
            database.  If ``instance`` is a new object then it will get saved to
            the database even if commit=False

            Returns ``instance``.
            """
            instance = super(BidirectionalM2MForm, self).save(commit=False)
            force_save = self.instance.pk is None
            if force_save:
                instance.save()
            for m2m_field, related_manager in self._get_bidirectional_m2m_fields():

#                setattr(self.instance, related_manager, self.cleaned_data[m2m_field])
#   direct assignment to the reverse side of a many-to-many set is prohibited. Use suppliers.set() instead.

                recordset = getattr(self.instance, m2m_field)
                records = recordset.all()
                # remove records that have been removed in form
                for record in records:
                    if record not in self.cleaned_data[m2m_field]:
                        recordset.remove(record)
                # add records that have been added in form
                for record in self.cleaned_data[m2m_field]:
                    if record not in records:
                        recordset.add(record)

            if commit:
                if not force_save:
                    instance.save()
                self.save_m2m()
            return instance

    return BidirectionalM2MForm

fixing the bug : "direct assignment to the reverse side of a many-to-many set is prohibited.
I use what I found here https://gist.github.com/Wtower/0b181cc06f816e4feac14e7c0aa2e9d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant