在做悦运动这个项目时,在创建约运动关系表时,运动开始时间,注意(导入datetime包时,我导入的是class datetime(__datetime.date)这个类,可以调用这个类中的方法,调用当前时间直接可以调用datetime.now()
1 from datetime import datetime 2 ...... 3 class Schedule(models.Model): 4 ''' 5 发起约运动 6 ''' 7 Status = ((1, '已完成'), (2, '待人加入'), (3, '已取消')) 8 user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name='发起人') 9 join_type = models.BooleanField(verbose_name='是否发起人') 10 now_people = models.IntegerField(verbose_name='已有人数', null=True, blank=True) 11 school = models.ForeignKey(Schools, on_delete=models.CASCADE, verbose_name='学校', default='') 12 sport = models.ForeignKey(Sports, on_delete=models.CASCADE, verbose_name='运动项目') 13 address = models.CharField(verbose_name='约定地点', max_length=100) 14 sport_time = models.DateTimeField(verbose_name='约定运动开始时间') 15 sport_end_time = models.DateTimeField(verbose_name='约定运动结束时间', default='') 16 people_nums = models.IntegerField(verbose_name='人数') 17 add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间') 18 status = models.IntegerField(choices=Status, verbose_name='状态') 19 20 class Meta: 21 verbose_name = '发起约运动' 22 verbose_name_plural = verbose_name 23 24 def __str__(self): 25 return self.sport.sport_name + '-' + str(self.sport_time) 26 27 28 class JoinSchedule(models.Model): 29 ''' 30 加入的运动 31 ''' 32 schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE, verbose_name='加入的运动') 33 user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name='用户') 34 add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间') 35 36 class Meta: 37 verbose_name = '加入运动' 38 verbose_name_plural = verbose_name 39 40 def __str__(self): 41 return str(self.schedule_id) + '--' + str(self.user_id)
而在序列化该表时,导入的是datetime:class datetime,包含了datetime这个模块所有方法,获得当前时间可以datetime.datetime.now(),如第十五行
1 import datetime 2 class MyJoinScheduleSerializer(serializers.ModelSerializer): 3 ''' 4 加入约运动序列化 5 ''' 6 user = serializers.HiddenField(default=serializers.CurrentUserDefault()) 7 add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M:%S') 8 9 def create(self, validated_data): 10 user = self.context['request'].user 11 schedule = validated_data['schedule'] 12 if Schedule.objects.filter(user=schedule.user, id=schedule.id).first().user == user: 13 raise serializers.ValidationError('您是该运动的发起人,已默认加入') 14 double_status = False 15 schedules = JoinSchedule.objects.filter(user=user,schedule__sport_time__gt=datetime.datetime.now()) 16 if schedules: 17 for s in schedules: 18 if s.schedule.sport_time > schedule.sport_end_time or schedule.sport_time >= s.schedule.sport_end_time: 19 pass 20 else: 21 double_status = True 22 break 23 if double_status: 24 raise serializers.ValidationError({"time_error":"与你已有的预约运动时间冲突"}) 25 else: 26 existed = JoinSchedule.objects.create(**validated_data) 27 return existed 28 29 class Meta: 30 model = JoinSchedule 31 fields = '__all__' 32 validators = [ 33 UniqueTogetherValidator( 34 queryset=JoinSchedule.objects.all(), 35 fields=('user', 'schedule'), 36 message='已经加入' 37 ) 38 ]
运行测试时会报错;type object 'datetime.datetime' has no attribute 'datetime'(对象'datetime.datetime'类型没有属性'datetime'),原因:
from datetime import datetime 已经相当于把命名空间引入到datetime.datetime一层,而在序列化时,又是import datetime,调用datetime.datetime.noe()会让编译器在datetime.datetime空间下去找datetime类。
但是datetime类是应该在datetime空间下的,所以会报错,主要是由于两个datetime不是一个等级的。