Created
December 28, 2022 21:08
-
-
Save J-Priebe/1939d700641151acb39eedaece92f606 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### models.py | |
from django.core.exceptions import PermissionDenied | |
from django.contrib.auth.models import AbstractUser | |
from django.db import models | |
from django.http import HttpResponse, Http404 | |
# your custom user model here | |
class MyCustomUser(AbstractUser): | |
is_patient = models.BooleanField() | |
is_doctor = models.BooleanField() | |
class Patient(models.Model): | |
# could also be a OneToOneField if the user can only have at most one patient/doctor | |
user = models.ForeignKey(MyCustomUser) | |
class Doctor(models.Model): | |
user = models.ForeignKey(MyCustomUser) | |
patients = models.ManyToManyField(Patient) | |
### views.py | |
def get_patients(request): | |
if not request.user.is_doctor: | |
raise PermissionDenied | |
try: | |
# for simplicity's sake, we'll assume a user can only | |
# have at most one doctor. If a user can have multiple Doctor "accounts", | |
# you would need to specify the one whose patients should be retrieved. | |
doctor = Doctor.objects.get(user=request.user) | |
except Doctor.DoesNotExist: | |
raise Http404("Not found") | |
patients = doctor.patients.all() | |
return render(request, 'patients.html', {'patients': patients}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment