从列表中选择用户并添加到发布请求 [英] Select users from list and add to post request
问题描述
我有一个用户列表,所有用户旁边都有一个复选框。所有选定的用户都应添加到数组中,然后通过Django发布请求发送到数据库。
I have a list of users that all have a checkbox next to them. All the users that are selected should be added to the array and then sent to the database via the Django post request.
以下是该图片:
底部的按钮是提交按钮。
The Button at the bottom is the submit button.
模型:
class UserProfile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
color = models.CharField(max_length=25, default="", blank=True)
class Studyplan(models.Model):
name = models.CharField(max_length=100, null=True)
description = models.CharField(max_length=1000, null=True)
parent_assignment = models.ForeignKey(Assignment, blank=True, on_delete=models.CASCADE,related_name="studyplan", null=True)
deadline = models.DateField(default=date.today, null=True)
students = models.ManyToManyField(UserProfile, blank=True, related_name="studyplan_students", null=True)
teachers = models.ManyToManyField(UserProfile, blank=True, related_name="studyplan_teachers", null=True)
canview = models.ManyToManyField(UserProfile, blank=True, related_name="studyplan_canview", null=True)
notes = models.ManyToManyField(Note, blank=True, related_name="studyplan", null=True)
tasks = models.ManyToManyField(Task, blank=True, related_name="studyplan", null=True)
messages = models.ManyToManyField(Message, blank=True, related_name="studyplan", null=True)
视图:
@login_required
def skapa_studieplan(request):
if request.method == 'POST':
name = request.POST.get('Studyplan-Name')
description = request.POST.get('Studyplan-Description')
parent_assignment = Assignment.objects.get(pk=request.POST.get('Studyplan-PAID'))
users = UserProfile.objects.all()
instance = Studyplan.objects.create(request.POST.get('Studyplan-Canview'))
for user in users:
instance.canview.add(user)
studyplan = Studyplan(name=name, description=description, parent_assignment=parent_assignment, canview=instance)
studyplan.save()
return redirect('allaStudieplaner')
我的问题是我无法弄清楚将ManyToManyField中的值放入POST请求中。
My problem is that I can't figure out to get the values from a ManyToManyField into a POST request.
我得到一个类似的错误:禁止直接分配给多对多集的正向。请使用emails_for_help.set()代替
I get an error similar to this: Direct assignment to the forward side of a many-to-many set is prohibited. Use emails_for_help.set() instead
选择模式:
{% for elev in elever %}
<div class="custom-control custom-checkbox">
<span class="d-flex align-items-center">
<div style="margin-right: 10px">
<div class="circle" style="background-color: {{ elev.color }}">
<span class="initials" , style="color: #ffffff"> {{ elev.user|capfirst|first }}
</span>
</div>
</div>
<div style="width: 300px">
<span class="h6 mb-0" data-filter-by="text">{{elev.user|capfirst}}</span>
</div>
<div class="checkboxx"> <input type="checkbox" name="Studyplan-Canview" value= "{{ student.name }}">
<style>
.checkerName {
margin-left: 10px
}
.checkboxx {
align-content: flex-end;
margin-left: 300px
}
</style> </div>
</span>
</label>
</div>
{% endfor %}
整个HTML:
The Whole HTML:
推荐答案
假设您的 request.POST
包含键 canview
的用户名列表,您将获得如下用户名列表: usernames = request .POST.getlist('canview')
。
Assuming your request.POST
contains a list of usernames for the key canview
, you'll get a list of the user name like this: usernames = request.POST.getlist('canview')
.
现在,使用用户名列表,您需要获取相应的用户并分配这些用户使用 set()
:
Now, with the list of usernames, you need to fetch the corresponding users and assign these to your M2M field using set()
:
usernames = request.POST.getlist('canview')
users = User.objects.filter(username__in=usernames)
studyplan = Studyplan(name=name, description=description, parent_assignment=parent_assignment)
studyplan.save() # you need a saved item to be able to create the M2M relationship
studyplan.canview.set(users)
这篇关于从列表中选择用户并添加到发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!