在一周中的几天内,使用python/django保存用户可用性的最佳方法 [英] Best way to save availability of user over days of week in python/django

查看:39
本文介绍了在一周中的几天内,使用python/django保存用户可用性的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储他可能有空的一周中某天的用户首选项.例如用户可以在周六,周日有空,但在其他天无空.目前,我正在使用由7个复选框组成的数组(值= 1,2,... 7),以便用户可以选择其有空的日子.

I want to store users preferences for day(s) of week he might be available. e.g. A user can be available on saturday, sunday but not on other days. Currently I am using array of 7 checkboxes(values=1,2,...7) so that user can select individual days of his availability.

现在,第一个问题是如何将其存储在数据库中.我正在考虑使用字符串(length = 7)并存储首选项,例如1100010,其中1表示可用,0表示不可用.这是好习惯吗?

Now the first question is how can i store this in database. I am thinking of using a string(length=7) and storing preferences like 1100010 where 1 will signify available and 0 not available. Is it good practice?

第二个问题,如何将POST数据(["1","2","7"])转换为字符串(1100010)

Second question, how can I convert POST data (["1","2","7"]) into string (1100010)

推荐答案

这有点棘手,但是鉴于工作日不会更改,如果您将具有每天名称的列添加到可用性表中,则可以使您的进一步代码更清晰0或1表示可用性.这似乎有点多余,但将来会更易于编码和维护.

It's kinda tricky, but given that weekdays are not going to change, your further code could be clearer if you add the column with each day's name into your availability table with 0 or 1 indicating availability. This may seem a bit redundant, but will be easier to code and maintain in the future.

第二个问题,如何将POST数据(["1","2","7"])转换为字符串(1100010)

Second question, how can I convert POST data (["1","2","7"]) into string (1100010)

week=['0']*7
for day in ["1","2","7"]:
    week[int(day) - 1]='1'
week=''.join(week)

这篇关于在一周中的几天内,使用python/django保存用户可用性的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆