Skip to content

direct assignment to the reverse side of a many-to-many set is prohibited. Use Name.set() instead. #4

@matinmidietsoir

Description

@matinmidietsoir

It's not quite an issue because I found a way to manage..

in extend.py add_bidirectional_m2m

I had this error "direct assignment to the reverse side of a many-to-many set is prohibited"
and I use the code found by https://gist.github.com/Wtower/0b181cc06f816e4feac14e7c0aa2e9d0

        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

and it works !

Thank you
(I'm a complete newbee, I don't know how to propose the change directly in the repository)


en français :
Ca n'est pas vraiment une erreur en fait, puisque j'ai trouvé la solution.
J'avais une erreur avec le code proposé et j'ai réussi à la contourner, avec le code précisé au-dessus, mais je ne sais pas comment proposer cet rectificatif directement sur le repository.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions