Select users from list and add to post request

浪尽此生 提交于 2019-12-13 08:08:47

问题


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.

Here is a picture of that:

The Button at the bottom is the submit button.

models:

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)

Views:

@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')

My problem is that I can't figure out to get the values from a ManyToManyField into a POST request.

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

The selection modal:

{% 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 %}

The Whole HTML:


回答1:


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').

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)


来源:https://stackoverflow.com/questions/57855194/select-users-from-list-and-add-to-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!