... <django.db.models.fields.related.foreignkey>处的TypeError不可序列化JSON

时间:2019-04-10 15:31:10

标签: django serialization django-rest-framework request

当我尝试从我的django api(或POST到)浏览器中获取时,抛出此消息:

/ api / cutarea-fca-use /中的

TypeError  不可序列化

那怎么了?一切都很好。

检查此api并将其与api比较,效果很好。检查我的正面。

和我的模型。py:

class FcaUse(models.Model): 
    id_fca = models.ForeignKey(Fca, blank=True, null=True, on_delete=models.DO_NOTHING)
    fell_form = models.ForeignKey(ShapeFelling, blank=True, null=True, on_delete=models.DO_NOTHING, verbose_name='kind1')
    fell_type = models.ForeignKey(TypeFelling, blank=True, null=True, on_delete=models.DO_NOTHING, verbose_name='kind2')
    main_type = models.ForeignKey(TypesForestry, blank=True, null=True, on_delete=models.DO_NOTHING, verbose_name='kind3') 
    sortiment = models.ForeignKey(ForestSort, blank=True, null=True, on_delete=models.DO_NOTHING, verbose_name='compos') 
    ed_izm = models.ForeignKey(UnitMeas, blank=True, null=True, on_delete=models.DO_NOTHING, verbose_name='values')
    vol_drew = models.FloatField(blank=True, null=True) 
    fca_res = models.SmallIntegerField(blank=True, null=True)
    vol_res = models.FloatField(blank=True, null=True)
    kind_id_id = models.ForeignKey(ForestKind, blank=True, on_delete=models.DO_NOTHING, verbose_name='group'),

class Meta:
        verbose_name = 'using'
        verbose_name_plural = 'usings'
        managed = True

UPD SERIALIZERS.PY HERE

class FcaUseSerializer(serializers.ModelSerializer):
    class Meta:
        model = FcaUse
        fields = ('id_fca','fell_form','fell_type','main_type','sortiment','ed_izm','vol_drew',\
        'fca_res','vol_res', 'kind_id_id')

UPD功能

def cutareause(request):
    if request.method == 'GET':
        queryset = FcaUse.objects.all()
        serializer = FcaUseSerializer (queryset, many= True)
        return JsonResponse(serializer.data, safe=False)

这是我的完整曲目:

Internal Server Error: /api/cutarea-fca-use/
Traceback (most recent call last):
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 158, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 156, in _get_response
    response = response.render()
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/renderers.py", line 724, in render
    context = self.get_context(data, accepted_media_type, renderer_context)
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/renderers.py", line 681, in get_context
    'content': self.get_content(renderer, data, accepted_media_type, renderer_context),
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/renderers.py", line 422, in get_content
    content = renderer.render(data, accepted_media_type, renderer_context)
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/renderers.py", line 105, in render
    allow_nan=not self.strict, separators=separators
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/utils/json.py", line 28, in dumps
    return json.dumps(*args, **kwargs)
  File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "/usr/lib/python3.5/json/encoder.py", line 200, in encode
    chunks = list(chunks)
  File "/usr/lib/python3.5/json/encoder.py", line 429, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.5/json/encoder.py", line 403, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.5/json/encoder.py", line 324, in _iterencode_list
    yield from chunks
  File "/usr/lib/python3.5/json/encoder.py", line 403, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.5/json/encoder.py", line 324, in _iterencode_list
    yield from chunks
  File "/usr/lib/python3.5/json/encoder.py", line 436, in _iterencode
    o = _default(o)
  File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/utils/encoders.py", line 68, in default
    return super(JSONEncoder, self).default(obj)
  File "/usr/lib/python3.5/json/encoder.py", line 179, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <django.db.models.fields.related.ForeignKey> is not JSON serializable

0 个答案:

没有答案