0

I am currently trying to create a health network website in Django. The idea is that there will be a class called User inside my registration application. One of the states stored inside User is which hospital the user is registered into.

I created another Hospital within the registration app. I want to user that model Hospital as one of the model field for the hospital_used state. How do I do that? Below is a portion of my UML that illustrates the relationship
UML Diagram

Below is a portion of my UML that illustrates the relationship png

Here is the code I have for it so far. The code where it is encapsulated with an asterisk is what I need help with.

class Hospital(models.Model):
    hospital_Name = models.CharField(max_length=150)

    def __str__(self):
        return "Hospital Name: " + str(self.hospital_Name)


class User(models.Model):
    PATIENT = 'Pat'
    DOCTOR = 'Doc'
    NURSE = 'Nurse'
    ADMINISTRATOR = 'Admin'
    user_type_choice = {
        (PATIENT, 'Patient'),
        (DOCTOR, 'Doctor'),
        (NURSE, 'Nurse'),
        (ADMINISTRATOR, 'Administrator'),
    }

    name = models.CharField(max_length=50)
    dob = models.DateField(auto_now=False)
    username = models.CharField(max_length=50)
    *preferred_hospital = Hospital(models.CharField(max_length=50))*
    patient_type = models.CharField(
        max_length=5,
        choices=user_type_choice,
    )

Thank you StackOverflow Buddies

2
  • Simply use a foreign key. Sep 23, 2016 at 13:47
  • wow, ok yeah thanks a lot
    – cruise_lab
    Sep 23, 2016 at 13:48

1 Answer 1

0

I would advise you to read this material on tutorials on how to create simple models.

What you want here is to use the ForeignKey method.

name = models.CharField(max_length=50)
dob = models.DateField(auto_now=False)
username = models.CharField(max_length=50)
preferred_hospital = models.ForeignKey(Hospital, on_delete = models.CASCADE)
patient_type = models.CharField(
    max_length=5,
    choices=user_type_choice,
)

You do not have to use on_delete = models.CASCADE but it is best that you handle what should happen when you delete an Hospital.

Know that you can also have OneToOne, ManyToOne, or ManyToMany fields, that are all described here.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.