只将DateTimeField的JSON可序列化部分拉入查询集?(Pull only a JSON serializable part of a DateTimeField into a querys

编程入门 行业动态 更新时间:2024-10-27 01:20:15
只将DateTimeField的JSON可序列化部分拉入查询集?(Pull only a JSON serializable part of a DateTimeField into a queryset? [duplicate])

这个问题在这里已有答案:

在Django 3答案 中将datetime序列化为json

将DateTimeField的JSON可序列化部分放入Django查询集的最有效方法是什么?

更大的图片是我为多个用户节省了能量计读数(IntegerField)+日期时间,并希望在django-graphos可视化中显示登录用户的数据。 理想情况下保持在日,周,月视图之间切换图形的灵活性。

我使用user_kwh整数和使用下面的user_id在我的视图中使用图表可以很好地为登录用户工作:

def graph(request): queryset = MeterReading.objects.filter(user=request.user) data_source = (ModelDataSource(queryset, fields=["user_id", "user_kwh"])) chart = gchart.LineChart(data_source) context = {'chart': chart} return render(request, 'dashboard/index.html', context)

但是当我将user_id更改为user_kwh_timestamp(我的datetime字段的名称)时,它会抛出TypeError:“datetime.datetime(2013,12,1,21,58,2,tzinfo =)不是JSON可序列化的”

我到目前为止尝试过的东西

我已经尝试将查询集中的值序列化,然后将它们传递给模板: data_source = serializers.serialize('json', MeterReading.objects.all(), fields=('user_kwh_timestamp',))但是这会产生一个AttributeError说'unicode'对象没有属性'get_header'。 我尝试使用.values_list定义更窄的查询集,但是说“'datetime.datetime'对象没有属性'user_kwh_timestamp'” 我已经找到了关于更新查询集中多个对象的Django文档,但它只显示了如何使用1个字符串更新所有对象: https : //docs.djangoproject.com/en/1.7/topics/db/queries/#updating-多个对象一次刻录

从Python datetime文档中看来,strftime()和%b或%x可能是解决方案的一部分,但此时我对如何/在何处实现这些https://docs.python.org/感到困惑。 2 /库/ datetime.html#日期时间的对象

我通过ide.c9.io使用Django 1.7.1和Python 2.7.6。 第一个编码项目和第一个问题,请原谅任何内容,风格或方法的无知:)

This question already has an answer here:

serialize datetime to json in Django 3 answers

What is the most efficient way to get a JSON serializable part of the DateTimeField into a Django queryset?

Bigger picture is that I'm saving energy meter readings (IntegerField) + datetimes for multiple users and would like to show a logged in user's data in a django-graphos visualization. Ideally keeping the flexibility to switch graphs between day, week, month views.

I've got the graph to work nicely for a logged in user with the user_kwh integer and the user_id using the below in my view:

def graph(request): queryset = MeterReading.objects.filter(user=request.user) data_source = (ModelDataSource(queryset, fields=["user_id", "user_kwh"])) chart = gchart.LineChart(data_source) context = {'chart': chart} return render(request, 'dashboard/index.html', context)

But when I change user_id to user_kwh_timestamp (my name of the datetime field) it throws the TypeError: "datetime.datetime(2013, 12, 1, 21, 58, 2, tzinfo=) is not JSON serializable"

Stuff I've tried so far

I've tried serializing the values in the queryset before passing them to the template with: data_source = serializers.serialize('json', MeterReading.objects.all(), fields=('user_kwh_timestamp',)) but that gives an AttributeError saying 'unicode' object has no attribute 'get_header'. I've tried defining the queryset narrower with .values_list but that says "'datetime.datetime' object has no attribute 'user_kwh_timestamp'" I've found this Django documentation on updating multiple objects in a queryset, but it only shows how to update all with 1 string: https://docs.djangoproject.com/en/1.7/topics/db/queries/#updating-multiple-objects-at-once

From the Python datetime documentation it seems that strftime() and %b or %x could be a part of the solution, but at this point I'm confused on how/where to implement those https://docs.python.org/2/library/datetime.html#datetime-objects

I'm using Django 1.7.1 and Python 2.7.6 via ide.c9.io. First coding project and first question here so excuse any ignorance in contents, style or approach :)

最满意答案

让json lib序列化结果值(来自queryset),指定一个JSON编码器 ,在本例中为DjangoJSONEncoder 。 您必须在序列化之前将查询集转换为列表,否则您可能会遇到一些异常。

import json from django.core.serializers.json import DjangoJSONEncoder ... def graph(request): queryset = MeterReading.objects.filter(user=request.user).values() data = json.dumps(list(queryset), cls=DjangoJSONEncoder) ...

Let json lib serialize the result values (from queryset), specifying a JSON encoder, in this case DjangoJSONEncoder. You must turn queryset into a list before serialize, or you may get some exception.

import json from django.core.serializers.json import DjangoJSONEncoder ... def graph(request): queryset = MeterReading.objects.filter(user=request.user).values() data = json.dumps(list(queryset), cls=DjangoJSONEncoder) ...

更多推荐

本文发布于:2023-08-02 06:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1370989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拉入   序列化   DateTimeField   JSON   Pull

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!