Issue
I have a single HTML form to fill up the details of both patient and his/her doctor. I have created a One to many relationship between Doctor and Patient models using ForeignKey.
The form(docpatient.html) goes like this:
<form action="{% url 'requestlanding' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h2>
Patient Details
</h2>
<label for="patientName">
Patient's Name:
</label>
<input type="text" id="patientName" name="patientName" required>
<label for="age">
Age:
</label>
<input type="number" id="age" name="age" required>
<label for="gender">
Gender:
</label>
<select id="gender" name="gender" required>
<option value="male">
Male
</option>
<option value="female">
Female
</option>
<option value="other">
Other
</option>
</select>
<label for="contactNumber">
Contact Number:
</label>
<input type="tel" id="contactNumber" name="contactPatient" required>
<label for="email">
Email:
</label>
<input type="email" id="email" name="emailPatient" required>
<label for="media">
Upload patient's picture:
</label>
<input type="file" id="media" name="wrecommend" accept="image/*,video/*,pdf/*">
<h2>
Doctor Details
</h2>
<label for="doctorName">
Doctor's Name:
</label>
<input type="text" id="doctorName" name="doctorName" required>
<label for="specialization">
Specialization:
</label>
<input type="text" name="specialization" id="specialization" required>
<label for="contactNumber">
Contact Number:
</label>
<input type="tel" id="contactNumber" name="contactDoctor" required>
<p>
(All the information must be correct and proper)
</p>
<button type="submit">
Submit
</button>
</form>
I have created Patient and Doctor models as following:
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
class Doctor(models.Model):
name = models.CharField(max_length=50, blank=False)
phone = models.IntegerField(
validators=[MinValueValidator(1000000000), MaxValueValidator(9999999999)],
blank=False,
)
specialization = models.CharField(max_length=50, blank=False, null=True)
def __str__(self):
return self.name
class Patient(models.Model):
GENDER = (("Male", "Male"), ("Female", "Female"), ("Others", "Others"))
name = models.CharField(max_length=50, blank=False, default=None)
email = models.EmailField(max_length=50, blank=False, default=None)
phone = models.IntegerField(
validators=[MinValueValidator(1000000000), MaxValueValidator(9999999999)],
blank=False,
default=None,
)
age = models.PositiveIntegerField(blank=False, null=True, default=0)
gender = models.CharField(max_length=6, choices=GENDER, blank=False, default=None)
doctor = models.ForeignKey(Doctor, on_delete=models.PROTECT)
wrecommend = models.FileField(
upload_to="wrecommend", max_length=50, blank=False, null=True
)
def __str__(self):
return self.name
Here is the view that I have created:
def requestlanding(request):
if request.method == "POST":
p_name = request.POST.get("patientName")
p_age = request.POST.get("age")
p_gender = request.POST.get("gender")
p_phone = request.POST.get("contactPatient")
p_email = request.POST.get("emailPatient")
p_wrecommend = request.POST.get("wrecommend")
d_name = request.POST.get("doctorName")
d_phone = request.POST.get("contactDoctor")
d_specialization = request.POST.get("specialization")
doctor = Doctor.objects.create(
name=d_name, phone=d_phone, specialization=d_specialization
)
doctor.save()
patient = Patient.objects.create(
doctor=doctor,
name=p_name,
email=p_email,
phone=p_phone,
age=p_age,
gender=p_gender,
wrecommend=p_wrecommend,
)
patient.save()
return redirect("requestlanding")
return render(request, "docpatient.html")
**I thought this satisfies the doctor-patient relation i.e. one to many relation but while adding two patients with the same doctor, the doctor's details in the database table is being duplicated. But the patients' name in the doctor's record should have been added.
I want the name of the patients of the doctor on the doctor's record.**
Please help me with this.
Solution
Sure, your code creates a doctor and patient every time the view is processed successfully, what you need to do is to check if the doctor exists before creating the object and this can be done by Django get_or_create
doctor = Doctor.objects.get_or_create(name=d_name, phone=d_phone, specialization=d_specialization)
#doctor.save()
Answered By - Mohamed ElKalioby
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.