在Django模型和序列化程序中指定AM和PM TimeField

时间:2019-06-29 19:00:04

标签: django django-models django-rest-framework

我有我的模特。py

class Restaurant(models.Model):
    name = models.CharField(max_length=100, blank=False)
    opening_time = models.TimeField(blank=False)
    closing_time = models.TimeField(blank=False)

    def __str__(self):
        return self.name

    @property
    def is_open(self):
        return True if self.opening_time <= datetime.now().time() < self.closing_time else False

然后,我的serializer.py:

class RestaurantSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Restaurant
        fields = ('pk', 'name', 'opening_time', 'closing_time')

如何输入我输入的时间是24小时还是上午和下午。

enter image description here

1 个答案:

答案 0 :(得分:1)

您的settings.py编辑日期时间格式

REST_FRAMEWORK = {
    'DATETIME_FORMAT': "%b %d at %I:%M %P"
}