1

Sorry I am beginer in django try to make app for appointment for doctors and could not solved this problem

I can't add value in to Appoiment Db in to django app this is my models.py

from django.db import models

models.py_

class Doctors(models.Model):
    doctor_name = models.CharField(max_length=80)
    specialization_doctor = models.CharField(max_length=80)

    def __str__(self):
        return self.doctor_name


class Appoiment(models.Model):
    doctor_name = models.ManyToManyField(Doctors)
    date_appoiment = models.DateField()
    time_appoiment = models.TimeField()
    patient_name = models.CharField(max_length=80)
    patient_phone = models.CharField(max_length=15)
    reason_for_visit = models.TextField(max_length=400)

    def __str__(self):
        return self.patient_name

And this is my views.py I'm trying many ways but steal can't approch to solve it.

VIEWS.PY

from django.shortcuts import render
from .models import Doctors,Appoiment
from .forms import AppoimentForm
from django.http import HttpResponseRedirect
def main(request):
    list_of_doctors = Doctors.objects.all().values('doctor_name')
    list_of_doctors = [item['doctor_name']for item in list_of_doctors]

    doctor_name=''
    date_appoiment=''
    time_appoiment=''
    name=''
    phone=''
    reason=''
    form = ''
    patient=''
    if request.method == 'POST':
            doctor_name = request.POST.get('doctor_name')
            date_appoiment = request.POST.get('date')
            time_appoiment = request.POST.get('time')
            name = request.POST.get('name')
            phone = request.POST.get('phone')
            reason = request.POST.get('reason')
            # doctor = Doctors()
# The problem is here how to write right statement
#------->>
            patient = Appoiment.objects.create(doctor_name=doctor_name,date_appoiment=date_appoiment,time_appoiment=time_appoiment,patient_name=name,patient_phone=phone,reason_for_visit=reason)



    return render(request,'main.html',{'list_of_doctors':list_of_doctors,'patient':patient})```


2
  • It seems like you're trying to create a new Appoiment object when a form is submitted, but you're encountering an issue with how to set the doctor_name field. Is it the issue? Commented Apr 2, 2024 at 9:41
  • If you add the patient.save() command the db should save this instance to the table. Commented Apr 2, 2024 at 13:33

2 Answers 2

1

In your form, you're getting the doctor_name as a string, but since it's a ManyToManyField you need to pass it as a list of Doctors objects.

So try this view:

from django.shortcuts import render
from .models import Doctors, Appoiment
from .forms import AppoimentForm
from django.http import HttpResponseRedirect

def main(request):
    list_of_doctors = Doctors.objects.all()

    if request.method == 'POST':
        doctor_names = request.POST.getlist('doctor_name')
        date_appoiment = request.POST.get('date')
        time_appoiment = request.POST.get('time')
        name = request.POST.get('name')
        phone = request.POST.get('phone')
        reason = request.POST.get('reason')
        
        
        doctors = [Doctors.objects.get(doctor_name=doc_name) for doc_name in doctor_names]

        
        appointment = Appoiment.objects.create(
            date_appoiment=date_appoiment,
            time_appoiment=time_appoiment,
            patient_name=name,
            patient_phone=phone,
            reason_for_visit=reason
        )
        appointment.doctor_name.add(*doctors)

        return HttpResponseRedirect('/')  

    return render(request, 'main.html', {'list_of_doctors': list_of_doctors})

Here:

At first, We use request.POST.getlist('doctor_name') to get a list of doctor names.

Then, we retrieve the corresponding Doctors objects from the database using Doctors.objects.get(doctor_name=doc_name).

Then, we create the Appoiment object using Appoiment.objects.create() and pass in the form data.

Finally, We add the selected doctors to the appointment object using appointment.doctor_name.add(*doctors).

Sign up to request clarification or add additional context in comments.

Comments

0

You can write this line as below,

patient = Appoiment(doctor_name=doctor_name,date_appoiment=date_appoimet,time_appoiment=time_appoiment,patient_name=name,patient_phone=phone,reason_for_visit=reason)
patient.save()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.