【发布时间】:2021-07-30 15:28:08
【问题描述】:
我正在开发一个 django 应用程序,并且我有一个带有 datetimefield 的模型,在我看来,我接受 json 格式的数据,其中包含时间戳中的日期,当我尝试将日期保存在我的模型中时出现此错误
"Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]."
我尝试在没有帮助的情况下使用它:
date_converted_to_date_time_field = datetime.utcfromtimestamp(due_in_time_stamp )
我的观点是这样的:
@api_view(['POST', ])
def todo_list_api_view(request):
if request.method == 'POST':
due_in_time_stamp = int(request.data["due"])
date_converted_to_date_time_field = datetime.utcfromtimestamp(due_in_time_stamp ) # i pass it to a serializer to save the data in a model like that
serializer = mySerializer(data=request.data , context = {'user':request.user , 'due_date' : date_converted_to_date_time_field})
有什么帮助吗?
【问题讨论】:
-
请分享一个JSON时间的例子。
-
@WillemVanOnsem 我的 json 时间“日期”示例:“1627656615”
-
您的问题似乎不在此处显示的代码中。您的
date_converted...变量将包含一个 Pythondatetime实例 - 之后您将如何处理它? -
我通过它来反序列化这些值并在 db @PeterDeGlopper 中创建一个新实例
标签: python django database rest