【发布时间】:2018-07-27 05:41:09
【问题描述】:
默认情况下,Django DateTime 在数据中保存并显示秒和毫秒以及额外信息,例如它保存并显示日期如下:
2018-07-21 05:27:29.736956+00:00
在尝试检索数据时,是否有任何方法或任何全局函数来忽略额外信息并仅获取日期和时间?喜欢
2018-07-21 05:27:29
型号:
class Enrolled(models.Model):
user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
product = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id',
related_name='enroll')
created_date = models.DateTimeField(auto_now_add=True)
序列化器
class EnrolledSerializer(ModelSerializer):
ptitle = serializers.SerializerMethodField()
instructor = serializers.SerializerMethodField()
instructorid = serializers.SerializerMethodField()
pslug = serializers.SerializerMethodField()
def get_instructor(self, object):
return object.product.author.username
def get_instructorid(self, object):
return object.product.author.id
def get_ptitle(self, obj):
return obj.product.title
def get_pslug(self, obj):
return obj.product.slug
class Meta:
model = Enrolled
fields = [
'id',
'user',
'product',
'ptitle',
'instructorid',
'pslug',
'instructor',
'created_date',
]
read_only_fields = ['product', 'created_date', 'id', 'user', 'instructor', 'instructorid', 'pslug', 'ptitle']
//编辑:
REST_FRAMEWORK = {
'DATETIME_FORMAT': ("%Y-%m-%d %H:%M"),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
}
USE_I18N = False
USE_L10N = False
USE_TZ = True
【问题讨论】:
标签: python django django-rest-framework