Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

django - 使用Django REST框架获取可用的预约预约时间(Get available time slots for appointment booking using Django REST framework)

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 .

(现在我有SalonAppointment模型和序列化器。)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...