禁止直接赋值给多对多集合的前端。使用 lab_test.set() 代替

Direct assignment to the forward side of a many-to-many set is prohibited. Use lab_test.set() instead

class Lab_Group(models.Model):
    group = models.CharField(max_length=100, unique=True,)

class Lab(models.Model):
    laboratory = models.CharField(max_length=50, unique=True)
    group = models.ForeignKey(Lab_Group, on_delete=models.CASCADE)

class LabRequest(models.Model):
    ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
    lab_test = models.ManyToManyField(Lab)

这是我的观点

def LabRequestToGenerateView(request, pk):
    doctor = Doctor.objects.filter(user_id=request.user.id, active='Yes')

    for doc in doctor:
        ticket = get_object_or_404(
            Ticket, pk=pk, has_cancelled=0, is_active=0, doctor=doc.id)
        lab_group = Lab_Group.objects.all()
        if request.POST.get('lab_test'):
            obj = LabRequest()
            obj.lab_test = request.POST.get('lab_test')
            obj.ticket = ticket
            obj.created_by = request.user.id
            obj.save()
            obj.save_m2m()
            messages.success(
                request, 'Lab Request has been sent successfully.')
            return redirect('/dashboard/lab-request')           
    context = {
        'title': 'Laboratory Request',
        'valueBtn': 'Save',
        'form': form,
        'rl': rl,
        'lab_group': lab_group

    }
    return render(request, 'dashboard/laboratory_request.html', context)

这是我的模板

<form action="." method="POST">
    {% csrf_token %}
    <div class="row">
        {% for lab_group in lab_group %}
        <div class="col-3 border">
            <label for="">{{ lab_group|upper }}</label>
            {% for lab in lab_group.lab_set.all %}
            {% if lab.active == 'Yes' %}
            <div class="form-check">
                <input type="checkbox" value="{{ lab.id }}" name="lab_test">
                {{ lab }}
            </div>
            {% endif %}
            {% endfor %}
        </div>
        {% endfor %}

    </div>
    <div class="form-group float-right">
        <button type="submit" name="submit" class="btn btn-success btn-sm" value=" {{ valueBtn }} "> <i
                class="fa fa-save"></i>
            Save</button>
    </div>
</form>

当我尝试保存它时显示此错误:

Direct assignment to the forward side of a many-to-many set is prohibited. Use lab_test.set() instead.

我卡了一个星期了,有什么问题可以随时问我。谢谢。

解决了你的问题。然而这里有一个解决方案。

def LabRequestToGenerateView(request, pk):
    doctors = Doctor.objects.filter(user_id=request.user.id, active='Yes')

    for doc in doctors:
        ticket = get_object_or_404(Ticket, pk=pk, has_cancelled=0, is_active=0, doctor=doc.id)
        lab_id = request.POST.get('lab_test')
        if lab_id is not None:
            obj = LabRequest.objects.create(ticket=ticket, created_by=request.user.id)
            lab = Lab.objects.get(id=lab_id)
            obj.lab_test.add(lab)
            obj.save()
            messages.success(request, 'Lab Request has been sent successfully.')
            return redirect('/dashboard/lab-request')

    lab_group = Lab_Group.objects.all()
    context = {
        'title': 'Laboratory Request',
        'valueBtn': 'Save',
        'form': form,
        'rl': rl,
        'lab_group': lab_group

    }
    return render(request, 'dashboard/laboratory_request.html', context)