0

I have a function based view that sends a post request to skapa-studieplan (a url) that is a function based view.

I want to know why I get this error: TypeError at /skapa_studieplan Direct assignment to the forward side of a many-to-many set is prohibited. Use students.set() instead.

The View:

#post request checker
    @login_required
    def skapa_studieplan(request):
        if request.method == 'POST':
            name = request.POST.get('Studyplan-Name')
            description = request.POST.get('Studyplan-Description')
            students = request.POST.get('Studyplan-Students')
            teachers = request.POST.get('Studyplan-Teachers')
            canview = request.POST.get('Studyplan-Canview')
            parent_assignment = request.POST.get('Studyplan-ParentAssignment')

            studyplan = Studyplan(name=name, description=description, students=request.user, teachers=request.user, canview=request.user, parent_assignment=parent_assignment)

            event.save()

    return redirect('allaStudieplaner')

in template:

 <form class="modal fade" id="project-add-modal" tabindex="-1" aria-hidden="true", action="{% url 'skapa-studieplan' %}" method='POST'>
          {% csrf_token %}
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <style>

also the studyplans model

class Studyplan(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=1000)
    parent_assignment = models.ForeignKey(Assignment, blank=True, on_delete=models.CASCADE,related_name="studyplan")
    deadline = models.DateField(default=date.today)
    students = models.ManyToManyField(UserProfile, blank=True, related_name="studyplan_students")
    teachers = models.ManyToManyField(UserProfile, blank=True, related_name="studyplan_teachers")
    canview = models.ManyToManyField(UserProfile, blank=True, related_name="studyplan_canview")
    notes = models.ManyToManyField(Note, blank=True, related_name="studyplan")
    tasks = models.ManyToManyField(Task, blank=True, related_name="studyplan")
    messages = models.ManyToManyField(Message, blank=True, related_name="studyplan")

class Meta:
   verbose_name_plural = 'Studyplans'

def __str__(self):
    return "{name}".format(name=self.name)

the urls:

urlpatterns = [
        path('', views.landingPage),
        path('login', views.loginPage),
        path('studyplans', views.studyplanPage, name='allaStudieplaner'),
        path('<int:pk>/view/detail/',views.detailStudyplanPage,name='detailStudyplanPage'),
        path('<int:pk>/view/detailTask/',views.detailTaskPage,name='detailTaskPage'),
        path('assignments', views.assignmentPage),
        path('skapa_studieplan', views.skapa_studieplan, name="skapa-studieplan"),
    ]
William Abrahamsson
  • 229
  • 1
  • 6
  • 19
  • check this [post](https://stackoverflow.com/questions/50015204/direct-assignment-to-the-forward-side-of-a-many-to-many-set-is-prohibited-use-e) – Nalin Dobhal Sep 06 '19 at 10:17
  • Possible duplicate of [Direct assignment to the forward side of a many-to-many set is prohibited. Use emails\_for\_help.set() instead](https://stackoverflow.com/questions/50015204/direct-assignment-to-the-forward-side-of-a-many-to-many-set-is-prohibited-use-e) – dirkgroten Sep 06 '19 at 10:24
  • 1
    Please search before asking. Your question is already answered on SO. Also the error tells you exactly what you should do: "use `students.set()`" (or use `students.add()`). – dirkgroten Sep 06 '19 at 10:25
  • can someone give an example on how to do this with a function based view? – William Abrahamsson Sep 06 '19 at 12:18
  • and with my example – William Abrahamsson Sep 06 '19 at 12:18

0 Answers0