这个问题在这里已有答案:
在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 answersWhat 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-onceFrom 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) ...更多推荐
发布评论