To create a doctor appointment application in Django, we'll outline the main models, views, and templates required for the application. This example will include patient medical history, consultations, medical prescriptions, patient medical aid status, and medical aid provider.
Models:
python
from django.db import models
from django.contrib.auth.models import User
class MedicalAidProvider(models.Model):
name = models.CharField(max_length=255)
class MedicalAidScheme(models.Model):
name = models.CharField(max_length=255)
category = models.CharField(max_length=255)
class Patient(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
medical_aid_provider = models.ForeignKey(MedicalAidProvider, null=True, blank=True, on_delete=models.SET_NULL)
medical_aid_scheme = models.ForeignKey(MedicalAidScheme, null=True, blank=True, on_delete=models.SET_NULL)
medical_aid_number = models.CharField(max_length=255, null=True, blank=True)
is_medical_aid_member = models.BooleanField(default=False)
class MedicalHistory(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
condition = models.CharField(max_length=255)
diagnosis = models.TextField()
date = models.DateField()
class Consultation(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
doctor = models.ForeignKey(User, on_delete=models.CASCADE, related_name='consultations')
date = models.DateField()
notes = models.TextField()
class Prescription(models.Model):
consultation = models.ForeignKey(Consultation, on_delete=models.CASCADE)
medication = models.CharField(max_length=255)
dosage = models.CharField(max_length=255)
frequency = models.CharField(max_length=255)
Views:
python
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Patient, MedicalHistory, Consultation, Prescription
class PatientListView(ListView):
model = Patient
class PatientDetailView(DetailView):
model = Patient
class ConsultationCreateView(CreateView):
model = Consultation
fields = ['date', 'notes']
class PrescriptionCreateView(CreateView):
model = Prescription
fields = ['medication', 'dosage', 'frequency']
class MedicalHistoryCreateView(CreateView):
model = MedicalHistory
fields = ['condition', 'diagnosis', 'date']
Templates:
You'll need to create templates for each view in the templates/appointment/ directory. For example, patient_list.html, patient_detail.html, consultation_create.html, prescription_create.html, and medical_history_create.html.
This example provides a basic structure for a doctor appointment application in Django. You can further customize and expand the application as needed.
User
def create_consultation(request, appointment_id): is incomplete
Assistant
Here's a complete version of the create_consultation function in Django:
python
from django.shortcuts import redirect, get_object_or_404
from .models import Appointment, Consultation
from .forms import ConsultationForm
def create_consultation(request, appointment_id):
appointment = get_object_or_404(Appointment, pk=appointment_id)
if request.method == 'POST':
form = ConsultationForm(request.POST)
if form.is_valid():
consultation = form.save(commit=False)
consultation.appointment = appointment
consultation.save()
return redirect('appointment_detail', appointment_id=appointment.id)
else:
form = ConsultationForm()
return render(request, 'appointment/consultation_form.html', {'form': form, 'appointment': appointment})
This function retrieves the appointment object using the provided appointment_id. If the request method is POST, it creates a new consultation using the provided form data, saves it, and redirects to the appointment detail page. If the request method is GET, it initializes a new, empty form.
You'll need to create a ConsultationForm form and a consultation_form.html template for this function to work properly.
Here's an example of a simple ConsultationForm:
python
from django import forms
from .models import Consultation
class ConsultationForm(forms.ModelForm):
class Meta:
model = Consultation
fields = ['date', 'notes']
And here's a simple consultation_form.html template:
You can then override the blocks in your specific templates, such as appointment_detail.html or consultation_form.html, to customize the content for each page.
For example, you can override the content block in consultation_form.html to display the form:
This way, the header, footer, and other common elements will be shared across all templates, while you can customize the content for each specific page.
User
To determine whether a patient is a: is incomplete
Assistant
To determine whether a patient is a medical aid member or a private patient, you can modify the Patient model as follows:
You can then use these methods in your views or templates to check the patient's status:
python
def appointment_detail(request, appointment_id):
appointment = get_object_or_404(Appointment, pk=appointment_id)
patient = appointment.patient
if patient.is_medical_aid():
# The patient is a medical aid member
pass
else:
# The patient is a private patient
pass
In your templates, you can use the is_medical_aid and is_private methods to conditionally display content:
html
{% if patient.is_medical_aid %}
<!-- Content for medical aid members -->
{% else %}
<!-- Content for private patients -->
{% endif %}
This way, you can easily check the patient's status and display or hide content accordingly.
User
continue
Assistant
To create a view for displaying the appointment details, you can create a appointment_detail view:
This view retrieves the appointment object, the patient associated with the appointment, and the consultations and prescriptions related to the appointment. It then passes this data to the appointment_detail.html template.
You'll need to create the appointment_detail.html template to display the appointment details:
This template displays the patient's name, medical aid information (if applicable), consultations, and prescriptions. Make sure to adjust the URL pattern to point to the appointment_detail function:
This template displays the patient's name, medical aid information (if applicable), and a list of appointments. For each appointment, it displays the appointment date and a link to the appointment detail page.
To pass the patient object to the patient_detail.html template, you can create a patient_detail view:
This way, the patient_detail.html template displays the patient's information and a list of appointments, and the appointment_detail.html template displays the appointment details, including consultations and prescriptions.