简体   繁体   中英

Django: How should I structure models for this?

UPDATE

I made these new models.

class Day(models.Model):
     day = models.CharField(max_length=20)


class Shift(models.Model):
    shift = models.CharField(max_length=20)
    days = models.ManyToManyField(Day, through='DayShift', related_name='day_shift')


class DayShift(models.Model):
    time = models.TimeField()
    day = models.ForeignKey(Day, related_name='to_day')
    shift = models.ForeignKey(Shift, related_name='to_shift')
    clinics_doctors = models.ManyToManyField(ClinicDoctor, through='ClinicDoctorDayShift', related_name='clinicDoctor_dayShift')

class ClinicDoctorDayShift(models.Model):
    clinic_doctor = models.ForeignKey(ClinicDoctor, related_name='to_clinicDoctor')
    day_shift = models.ForeignKey(DayShift, related_name='to_dayShift')

ORIGINAL QUESTION

I am trying to make my first website. I am using Django for it.

I have these models right now related to which I have question:

class Clinic(models.Model):
    name = models.CharField(max_length=100)

    DOC_DAYS_CHOICES = (
       'Sunday',
       'Monday',
       'Tuesday',
       'Wednesday',
       'Thursday',
       'Friday',
       'Saturday'
    )

   DOC_DAYS_SHIFTS = (
      'Morning',
      'Afternoon',
      'Evening'
   )

class ClinicDoctor(models.Model):
   doctor = models.ForeignKey('User', related_name='doctorsF') 
   clinic = models.ForeignKey(Clinic, related_name='clinicsF')
   days = models.CharField(max_length=200)  
   time = models.TimeField()
   shift = models.CharField(max_length=15)

I am stuck with this thing. I have been modifying the models as I go along. Now I don't know how to achieve what I want. I have attached two images. One shows how my data will be and another shows how I would like it to appear on the page.

I would like to structure the models such that code is efficient that what I want to display should make a lesser DB hits anything I can achieve with perfetch ?

结构图 页面显示

As the image shows. Each doctor has multiple clinics. Then 7 days of the week he can have 3 shifts per day for each clinic.

Try to create a table for DOC_DAYS_CHOICES & DOC_DAYS_SHIFTS , because every time you need to match the string, instead of that you can match the ID as well as you can use this dropdown suggestion. It's easy for you maintain the relations in ClinicDoctor table . It's not perfect solutions, it's suggestions to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM