简体   繁体   中英

Store list of tuples django model in easy to access way

I'm creating django application for online Barber Shop booking. I came out with an algorithm that store avaliable hours. Here is exaple how it works. Let's say that barber_1 works form 8am to 4pm. We store this date in list with tuple [(8, 16)] if someone book an 1 hour apointment for 1pm it checks if it is between tuple and if it is, it creates 2 different tuples with updated hours [(8, 13), (14, 16)], if another apointment is booked for let's say 10am, than it deletes first tuple and creates two new on it's place [(8, 10), (11, 13), (14, 16)] and so on and so on. I have a problem with "translating" my python code to the Django model. Can't find a way to store list of tuples in accesable way to be able to edit it. I read on different post that list of tuples can be stored in many to one relation but I dont think that it will be good in my case.

Use JSONField but it is actually a textfield, I personally would convert the list into json format then store it as text in your model

You can do a many-to-one relation between a barber table and an appointments table. The appointments would each have a barber id foreign key, a start time, and an end time column.

appointment_id barber_id start end
1 1 8 10
2 1 11 13
3 1 14 16

You'd probably want to keep an index on the barber_id .

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