I am trying to develop an API for scheduling appointments for several salons.
(我正在尝试开发一个API,用于安排数家沙龙的约会。)
Right now I have models and serializers for the Salon
and the Appointment
.
(现在我有Salon
和Appointment
模型和序列化器。)
All appointments can have different durations. (所有约会的持续时间可以不同。)
The Appointment
model looks like this: (Appointment
模型如下所示:)
class Appointment(models.Model):
date = models.DateField()
start_time = models.TimeField()
end_time = models.TimeField()
salon = models.ForeignKey(Salon, on_delete=models.CASCADE)
How can I return all salons and their available time slots (no appointment at that time) for a given date.
(如何退回给定日期的所有沙龙及其可用的时间段(当时没有预约)。)
So say if a salon is open from 10:00-16:00 and has only one appointment from 11:00-11:30 we would get something like: (假设一家沙龙从10:00-16:00开放,并且只有11:00-11:30预约,我们会得到以下信息:)
{
"id": 3,
"name": "My Salon",
"city": "London",
"time_slots": [
{
"date": "2019-12-01",
"start_time": "10:00:00",
"end_time": "11:00:00",
},
{
"date": "2019-12-01",
"start_time": "11:30:00",
"end_time": "16:00:00",
}
]
}
Right now the only way I can think of is by adding a new TimeSlot
model with Salon
as Foreign key and saving all time slots in the database and changing them once an appointment is made?
(现在我唯一能想到的方法是添加一个新的带有Salon
作为外键的TimeSlot
模型,并将所有时隙保存在数据库中,并在约会后更改它们?)
However, this does not seem very efficient. (但是,这似乎不是很有效。)
Is there a better way of calculating and returning the time slots?
(有没有更好的方法来计算和返回时隙?)
Edit
(编辑)
Serializers:
(序列化器:)
class AppointmentSerializer(serializers.ModelSerializer):
class Meta:
model = Appointment
fields = ('id', 'date', 'start_time', 'end_time', 'salon')
class SalonSerializer(serializers.ModelSerializer):
appointment_set = AppointmentSerializer(many=True, read_only=True)
class Meta:
model = Salon
fields = ('id', 'name', 'city', 'appointment_set')
depth = 1
ask by Imaginaryy translate from so