How can I show payment update for a specific appointment?

Question:

So I’m trying to make a section under each appointment details that shows their payment transactions of the appointment specifically. all my tries so far didn’t work, i can only show all payment updates which is not the wanted result obviously.
this is the code:

views.py the view for updating payments

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

the one I’m trying to make it work

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

This is the class the makes the updates

class PaymentUpDate(models.Model):
    payment_date = models.DateTimeField(auto_now_add=True)
    payment_amount = models.CharField(max_length=550)

I’ve tried doing the same approach as I did to show a specific appointment, which is:

appointment = get_object_or_404(Appointment, id = id)

to the updates, which looked something like :

updates = get_object_or_404(PaymentUpDate, id = id)

which resulted in the following error :

TypeError at /appointment-details/3
'PaymentUpDate' object is not iterable

and i also tried this guide: How to show every user specific payment for them in django?, which made my code look like this:

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)

which resulted in not getting any updates for some reason.

Asked By: Smokar

||

Answers:

You can try belew querysets, one of which @raphael already mentioned in above comment.

PaymentUpDate.objects.filter(appointment=appointment)

Or:

PaymentUpDate.objects.filter(appointment_id=id)

But I seriously think that ...filter(appointment=id) should also work can you please check whether the PaymentUpDate model has the instances of Appointment model or not as you have null=True in ForeignKey (then I think you should also have blank=True).

Answered By: Sunderam Dubey

My initial comment was incorrect. The answer Sunderam gave is correct. There is no output because the update is null. The only place I can see where you actually create a PymentUpDate is:

#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()

Here you create a new PaymentUpDate object, but you have not given it an appointment field value, so it defaults to null. Therefore when you search for a PaymentUpDate instance connected to a particular appointment, it returns nothing, no output.

So, change it to:

#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()

You have a lot of if...elif...else. Not criticizing, but if my answer still does not help, try adding print statements at various locations in your logic, to see if you are indeed getting to where you want, and if new PaymentUpDate objects are actually being produced.

Answered By: raphael