因此,我试图在每个约会详细信息下创建一个部分,具体显示约会的付款交易。到目前为止我所有的尝试都没有成功,我只能显示所有的付款更新,这显然不是我想要的结果。这是代码:
views.py更新付款的视图
def edit_payment(request, id):
search_patient_form = SearchPatient()
current_user_phone = request.session.get('user')
current_user = get_object_or_404(Doctor, phone = current_user_phone)
if current_user:
appointment = get_object_or_404(Appointment, id=id)
if request.method == 'POST':
form = AddAppointmentForm(request.POST)
searchPatient = SearchPatient(request.POST)
if searchPatient.is_valid():
patient_id_phone = searchPatient.cleaned_data['patient_id_phone']
return redirect('search_patient', id_phone=patient_id_phone)
elif form.is_valid():
patient_phone = form.cleaned_data['patient_phone']
patient = Patient.objects.filter(phone = patient_phone).first()
doctor_select = form.cleaned_data['doctor_field']
doctor = Doctor.objects.filter(username = doctor_select).first()
appointment_date = form.cleaned_data['appointment_date']
time_slot_field = form.cleaned_data['time_slot_field']
#time_slot = TimeSlot.objects.filter(slotname=time_slot_field).first()
appointment_description = form.cleaned_data['appointment_description']
service_name = form.cleaned_data['service_name']
total_price = form.cleaned_data['total_price']
upfront_payment = form.cleaned_data['upfront_payment']
grand_total = form.cleaned_data['grand_total']
discount = form.cleaned_data['discount']
payment_type = form.cleaned_data['payment_type']
appointment.patient = patient
appointment.doctor_of_appointment = doctor
appointment.appointment_date = appointment_date
appointment.receptionist_of_appointment = current_user
appointment.appointment_timeslot = time_slot_field
appointment.appointment_description = appointment_description
appointment.service_name = service_name
appointment.total_price = total_price
appointment.upfront_payment = upfront_payment
appointment.grand_total = grand_total
appointment.discount = discount
appointment.payment_type = payment_type
appointment.save()
new_log = Log(
log_user = current_user,
log_descripton = 'Appointment has been editted. {0}'.format(
str(id)),
)
new_log.save()
#Payment Update Section
new_update = PaymentUpDate(
payment_amount = 'Total Price : {0} - Patient Payment: {1} - Balance: {2}'.format(
total_price,upfront_payment,grand_total
)
)
new_update.save()
return redirect('appointment_details', id=appointment.id)
else:
form = AddAppointmentForm(initial={
'patient_phone' : appointment.patient.phone,
'doctor_field' : appointment.doctor_of_appointment.username,
'appointment_date' : appointment.appointment_date,
'time_slot_field' : appointment.appointment_timeslot,
'appointment_description' : appointment.appointment_description,
'service_name': appointment.service_name,
'total_price': appointment.total_price,
'upfront_payment': appointment.upfront_payment,
'grand_total': appointment.grand_total,
'discount': appointment.discount,
'payment_type': appointment.payment_type,
})
return render(request, 'edit-payment.html', {
'current_user': current_user,
'sayfa': 'appointments',
'form': form,
'search_patient_form': search_patient_form
})
else:
return redirect('login')
我想让它工作
def appointment_details(request, id):
search_patient_form = SearchPatient()
current_user_phone = request.session.get('user')
current_user = get_object_or_404(Doctor, phone = current_user_phone)
if current_user:
if request.method == 'POST':
searchPatient = SearchPatient(request.POST)
if searchPatient.is_valid():
patient_id_phone = searchPatient.cleaned_data['patient_id_phone']
return redirect('search_patient', id_phone=patient_id_phone)
else:
appointment = get_object_or_404(Appointment, id = id)
updates = PaymentUpDate.objects.all()
return render(request, 'about-appointment.html', {
'current_user': current_user,
'sayfa': 'appointments',
'appointment': appointment,
'updates': updates,
'search_patient_form': search_patient_form
})
else:
return redirect('login')
models.py
这是进行更新的类
class PaymentUpDate(models.Model):
payment_date = models.DateTimeField(auto_now_add=True)
payment_amount = models.CharField(max_length=550)
我试着用同样的方法来显示一个特定的约会,这是:
appointment = get_object_or_404(Appointment, id = id)
到更新,看起来像:
updates = get_object_or_404(PaymentUpDate, id = id)
,导致以下错误:
TypeError at /appointment-details/3
'PaymentUpDate' object is not iterable
和我也尝试了这个指南:如何在django中显示每个用户的特定支付?,使我的代码看起来像这样:
class PaymentUpDate(models.Model):
payment_date = models.DateTimeField(auto_now_add=True)
payment_amount = models.CharField(max_length=550, null=True)
appointment = models.ForeignKey(Appointment, on_delete = models.CASCADE, null=True)
views.py
updates = PaymentUpDate.objects.filter(appointment=id)
由于某些原因导致没有得到任何更新。
我最初的评论是错误的。桑德拉姆给出的答案是正确的。没有输出,因为更新为空。我能看到你真正创建PymentUpDate的唯一地方是:
#Payment Update Section
new_update = PaymentUpDate(
payment_amount = 'Total Price : {0} - Patient Payment: {1} - Balance: {2}'.format(
total_price,upfront_payment,grand_total
)
)
new_update.save()
这里您创建了一个新的PaymentUpDate对象,但是您没有给它一个appointment
字段值,所以它默认为null。因此,当您搜索连接到特定约会的PaymentUpDate实例时,它什么也不返回,没有输出。
所以改成:
#Payment Update Section
new_update = PaymentUpDate(
payment_amount = 'Total Price : {0} - Patient Payment: {1} - Balance: {2}'.format(
total_price,upfront_payment,grand_total
),
appointment=appointment
)
new_update.save()
你有很多if...elif...else
。不是批评,但如果我的答案仍然没有帮助,尝试在逻辑的各个位置添加print
语句,看看你是否确实到达了你想要的地方,如果新的PaymentUpDate对象实际上正在产生。
你可以尝试下面的查询集,其中一个@raphael已经在上面的评论中提到了。
PaymentUpDate.objects.filter(appointment=appointment)
或:
PaymentUpDate.objects.filter(appointment_id=id)
但我认真地认为...filter(appointment=id)
也应该工作,你可以检查PaymentUpDate
模型是否具有Appointment
模型的实例,因为你在ForeignKey中有null=True
(然后我认为你也应该有blank=True
)。