Home > Back-end >  'ManyRelatedManager' object has no attribute 'save'
'ManyRelatedManager' object has no attribute 'save'

Time:07-25

I've got the same problem as this post. I tried the solution from cshelly, but my adoption fails on the last line given (the save). The difference is my code uses model based views and forms and has a polymorphic model. It's throwing an exception on the last line, where a save of the append to the manytomany field. I use whatever django gives as the through table. I can see the value being added. If I comment out the offending line, the match object has everything from the form (including the objects selected on the form from the other (in this case, the student) manytomany field.

All help is much appreciated.

Here's the view:

        if form.is_valid():

            instance = form.save(commit=False)
            current_user = self.request.user
            instance.lvm_affiliate = current_user.affiliate
            instance.save()

            if pt == 'tutor':
                person = Tutor.objects.get(id=pk)
                print("before ", vars(instance))
                print("student ", instance.student.all())
                print("tutor ", instance.tutor.all())
                instance.tutor.add(person.id)
                print("after ", vars(instance))
                print("student ", instance.student.all())
                print("tutor ", instance.tutor.all())
                instance.save()
                instance.tutor.save()

here's the output:

before  {'_state': <django.db.models.base.ModelState object at 0x000001E84D5CEAA0>, 'id': 46, 'lvm_affiliate_id': 34, 'match_status_id': 3, 'date_started': datetime.date(2022, 7, 23), 'date_dissolved': None, 'comments': ''} 
student <PolymorphicQuerySet []> 
tutor  <PolymorphicQuerySet []> 
after {'_state': <django.db.models.base.ModelState object at 0x000001E84D5CEAA0>, 'id': 46, 'lvm_affiliate_id': 34, 'match_status_id': 3, 'date_started': datetime.date(2022, 7, 23), 'date_dissolved': None, 'comments': ''} 
student <PolymorphicQuerySet []> 
tutor  <PolymorphicQuerySet [<Tutor: joey>]>
Internal Server Error: /match/filter/9/tutor/

'ManyRelatedManager' object has no attribute 'save'

Here's the model in case it's helpful:

class Match(models.Model):

    lvm_affiliate = models.ForeignKey(Affiliate, on_delete=models.SET_NULL, null=True,  blank=False)

    tutor = models.ManyToManyField(Tutor, blank=True, help_text="The tutor(s) associated with this match.")
    student = models.ManyToManyField(Student, blank=True, help_text="The student(s) associated with this match.")

CodePudding user response:

Delete this line: instance.tutor.save()

The code should work without saving m2m relation manually - if I remember correctly .add() does it automatically.

CodePudding user response:

While trying many combinations of add() and set() to the ManyToManyField and when save() was called, nothing worked. So I tried a different tact and the following worked. Not pretty, but it works.

        if form.is_valid():
            instance = Match.objects.create(match_status=form.cleaned_data['match_status'],
                                            date_started=form.cleaned_data['date_started'],
                                            comments=form.cleaned_data['comments'],
                                            lvm_affiliate = self.request.user.affiliate,)
            if pt == 'tutor':
                for student in form.cleaned_data['student']:
                    instance.student.add(student)

                person = Tutor.objects.get(id=pk)
                instance.tutor.add(person)

                instance.save()
  • Related