本文介绍了Django序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试序列化我的django模型:

 >>>来自studentapp.models import StudentAcademicPrograms 
>>>来自studentapp.models import AcademicPrograms,AcademicProgramsManager
>>>来自django.core import serializers
>>>> json_serializer = serializers.get_serializer(json)
>>> data = json_serializer.serialize(StudentAcademicPrograms.objects.all())

但是我收到这个错误:

 追溯(最近的最近通话):
文件< console>,第1行,模块>
TypeError:未绑定的方法serialize()必须使用Serializer实例作为第一个参数调用(取而代之的是QuerySet实例)


解决方案

您可以这样做:

  from django.core .serializers import serialize 
serialize('json',StudentAcademicPrograms.objects.all())

您的具体问题是您尝试在$ code> Serializer 类调用 serialize()类,而不是实例。 p>

  JSONSerializer = serializers.get_serializer(json)
json_serializer = JSONSerializer()
json_serializer.serialize(StudentAcademicPrograms .objects.all())
data = json_serializer.getvalue()

还查看来自django docs的。 p>

I am trying to serialize my django model like this:

>>> from studentapp.models import StudentAcademicPrograms
>>> from studentapp.models import AcademicPrograms, AcademicProgramsManager
>>> from django.core import serializers
>>> json_serializer = serializers.get_serializer("json")
>>> data = json_serializer.serialize(StudentAcademicPrograms.objects.all())

But i am getting this error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: unbound method serialize() must be called with Serializer instance as first argument (got QuerySet instance instead)
解决方案

You can do it this way:

from django.core.serializers import serialize
serialize('json', StudentAcademicPrograms.objects.all())

Your specific problem is that you are try to call serialize() on a Serializer class, not instance.

JSONSerializer = serializers.get_serializer("json")
json_serializer = JSONSerializer()
json_serializer.serialize(StudentAcademicPrograms.objects.all())
data = json_serializer.getvalue()

Also check out this example from django docs.

这篇关于Django序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:32