当表单提交时,我想自动将提交表单的用户添加到下面给出的模型上的多对多用户字段中,我该如何从视图中进行操作?
该模型:
class Project(MainAbstractModel):
users = models.ManyToManyField(User)
title = models.CharField(max_length=25, default="Conflict")
风景:
def myconflicts(request):
if request.method == "POST":
form = ProjectForm(request.POST)
if form.is_valid():
form.save()
else:
form = ProjectForm()
return render(request, 'conflictmanagement/myconflicts.html')
我的形式很简单:
class ProjectForm(ModelForm):
class Meta:
model = Project
fields = ["title"]
您可以在视图中添加用户,例如: