# 数据表的设计 class ProMeetTable(models.Model): """ 个人安排表 """ user = models.ForeignKey(to="UserInfo",verbose_name="所属用户") week_choice = ( (1,"Monday"), (2,"Tuesday"), (3,"Wednesday"), (4,"Thursday"), (5,"Friday"), ) week = models.IntegerField(choices=week_choice,verbose_name="星期") time_choice = ( (1, "08:00"), (2, "09:00"), (3, "10:00"), (4, "11:00"), (5, "12:00"), (6, "13:00"), (7, "14:00"), (8, "15:00"), (9, "16:00"), (10, "17:00"), (11, "18:00"), ) time = models.IntegerField(choices=week_choice, verbose_name="时刻") title = models.CharField(max_length=250,verbose_name="安排",null=True,blank=True) def __str__(self): return self.title class Meta: verbose_name = "ProMeetTable" verbose_name_plural = verbose_name db_table = 'ProMeetTable'
# 假设设计好的个人安排数据
# 接口:
class ProData(APIView): authentication_classes = [] def post(self,request): message = {} week= [1,2,3,4,5] id = int(request.data.get("id")) ProTime = [] # 遍历每个时刻 for x in [8,9,10,11,12,13,14,15,16,17,18]: ProMeetTableRaw = models.ProMeetTable.objects.filter(user_id=id,time=x).all() week_plan = [] for table in ProMeetTableRaw: week_plan.append(table.week) # 遍历计算出每个时刻空内容的星期 创建一个空包在数据库内 for week_set in set(week).difference(set(week_plan)): models.ProMeetTable.objects.create(week=week_set,time=x,user_id=id,title="") # 根据时刻 将周一到周五全部抽出来 ProMeetTableSet = models.ProMeetTable.objects.filter(user_id=id,time=x).all().order_by("week") data_f = {} data_f["time"] = "{}:00".format(x) for table in ProMeetTableSet: data_f[self.type_week(table.week)] = table.title ProTime.append(data_f) # 删除所有空字段 models.ProMeetTable.objects.filter(user_id=id, title="").all().delete() message['code'] = 200 message['data'] = ProTime return JsonResponse(message) def type_time(self, item): data = {8: "08:00",9: "09:00",10: "10:00",11: "11:00",12: "12:00",13: "13:00",14: "14:00", 15: "15:00",16: "16:00",17: "17:00",18: "18:00",} res = data[item] return res def type_week(self, item): data = {1: "Monday",2: "Tuesday",3: "Wednesday",4: "Thursday",5: "Friday",} res = data[item] return res
# 数据渲染
总结:有时候数据结构不方便的时候可以借助数据库(这里建议使用内存型数据库)来整理数据结构也是不错的选择