Django Rest Framework中模型的动态字段

时间:2018-06-22 16:12:19

标签: python django django-models django-rest-framework

(使用Django 2.0,Python 3.6和Django Rest Framework 3.8)

我正在尝试创建一个约会计划API,其中Client可以根据UserB的可用性来安排与UserB的会话。 UserB有一个字段,显示其在UserB模型中的可用性(除了其他字段)。

UserB模型如下:

class Users(models.Model):

    profile = models.OneToOneField(TriUser, on_delete=models.CASCADE)

    trainer_certifications = models.TextField(max_length=5000, default='')

    trainer_bio = models.TextField(max_length=5000, default='')

    trainer_profile_pic = models.ImageField(upload_to="gyminfofiles/trainers/images/", null=True, blank=True)

    gym = models.ForeignKey(Gym, on_delete=models.CASCADE, default = '', null=True, blank=True, related_name='GymClassRelatedName')

    workout_types = models.ManyToManyField(WorkoutTypes,)

    availability = models.ManyToManyField('Availability', related_name='AvailabilityRelatedName', blank=True)

    trainer_bookings = models.ManyToManyField('BookAppointment', related_name='BookAppointmentRelatedName', blank=True)


    def __str__(self):
        return str(self.profile)

Availability模型的下面可以看到:

class Availability(models.Model):

    SUNDAY = 'SUNDAY'
    MONDAY = 'MONDAY'
    TUESDAY = 'TUESDAY'
    WEDNESDAY = 'WEDNESDAY'
    THURSDAY = 'THURSDAY'
    FRIDAY = 'FRIDAY'
    SATURDAY = 'SATURDAY'

    AVAILABILITY_DATE_CHOICES = (
        (SUNDAY, 'Sunday'),
        (MONDAY, 'Monday'),
        (TUESDAY, 'Tuesday'),
        (WEDNESDAY, 'Wednesday'),
        (THURSDAY, 'Thursday'),
        (FRIDAY, 'Friday'),
        (SATURDAY, 'Saturday'),
    )
    availability_date = models.CharField(
        max_length = 9,
        choices=AVAILABILITY_DATE_CHOICES
    )

    T1200_1230_AM = '12:00 am - 12:30 am'
    T1230_100_AM = '12:30 am - 1:00 am'
    T100_130_AM = '1:00 am - 1:30 am'
    T130_200_AM = '1:30 am - 2:00 am'
    T200_230_AM = '2:00 am - 2:30 am'
    T230_300_AM = '2:30 am - 3:00 am'

    AVAILABILITY_TIME_CHOICES = (
        (T1200_1230_AM, '12:00 am - 12:30 am'),
        (T1230_100_AM, '12:30 am - 1:00 am'),
        (T100_130_AM, '1:00 am - 1:30 am'),
        (T130_200_AM, '1:30 am - 2:00 am'),
        (T200_230_AM, '2:00 am - 2:30 am'),
        (T230_300_AM, '2:30 am - 3:00 am'),
    )
    availability_time = models.CharField(
        max_length=18,
        choices=AVAILABILITY_TIME_CHOICES
    )


    def __str__(self):
        return str((self.availability_date, self.availability_time))

Client尝试安排约会时,会将他们定向到具有以下结构的模型:

class BookAppointment(models.Model): User_to_schedule_with = models.OneToOneField(Users) AvailableUserTimes_to_choose_from = (THIS IS WHER I'M STUCK)

其中User_to_schedule_with字段将是一个下拉列表,其中显示客户端可以安排约会的各种用户(例如:UserB,UserC,UserD等)。客户端选择用户后,我希望在字段AvailableUserTimes_to_choose_from中填充特定于所选用户的可用性。我将如何完成此工作,如何从availability模型访问一个特定字段UserB

UserB模型如下:

非常感谢您的帮助。

0 个答案:

没有答案