实验室课程
class Lab(models.Model):
laboratory = models.CharField(max_length=50, unique=True)
group = models.ForeignKey(Lab_Group, on_delete=models.CASCADE)
LabRequest类
class LabRequest(models.Model):
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
lab_test = models.ManyToManyField(Lab)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
这就是我保存数据时的样子
@login_required
def LabRequestToGenerateView(request, pk):
lab_group = ''
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.getlist('lab_test'):
lab_test = request.POST.getlist('lab_test')
laboratory = Lab.objects.filter(id__in=lab_test)
lab_test_id = LabRequest.objects.filter(examined=0, paid=0, lab_test__in=lab_test, ticket__patient=ticket.patient.id)
if lab_test_id:
messages.success(
request, 'Some lab tests have not been examined.')
return redirect('/dashboard/lab-request')
else:
instance = LabRequest.objects.create(
ticket=ticket, created_by=request.user.id)
instance.lab_test.add(*laboratory)
messages.success(
request, 'Lab Request has been sent successfully.')
return redirect('/dashboard/lab-request')
context = {
'title': 'Laboratory Request',
'valueBtn': 'Save',
'lab_group': lab_group
}
return render(request, 'dashboard/laboratory_request.html', context)
这是我尝试更新LabRequest类的方式,但由于我不知道如何更新,因此还不完整:
@login_required
def LabRequestUpdateView(request, pk):
doctor = Doctor.objects.filter(user_id=request.user.id, active='Yes')
for doc in doctor:
lab_request_id = get_object_or_404(LabRequest, pk=pk, ticket__doctor=doc.id)
context = {
'title': 'Update Request Lab',
'valueBtn': 'Update',
'lab_request_id': lab_request_id
}
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>
因此,我想更新 LabRequest 类模型,但是我不知道如何使用,在尝试更新时我一直在使用Forms.py,但这很容易,但是这种方式是新的对我来说。