我有一个基于功能的视图,该视图向基于功能的视图skapa-studieplan(一个URL)发送一个发布请求。
我想知道为什么会出现此错误:
/ skapa_studieplan
处的TypeError
禁止直接分配给多对多集的前端。请改用students.set()
。
视图:
#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')
在模板中:
<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>
也是学习计划模型
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)
网址:
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"),
]