<fieldfile:none =“”>不是JSON可序列化的

时间:2015-08-10 03:03:17

标签: python json django

我从不使用这样的FieldFile。我不知道为什么会发生此消息。

无论如何,我在下面显示我的相关代码片段,

Models.py&GT;

class MW_part(models.Model):
    name = models.CharField(max_length=100)
    code = models.CharField(max_length=100, null=True, blank=True)
    manufacture = models.CharField(max_length=50, null=True, blank=True)
    spec = models.TextField(max_length=1024, null=True, blank=True)
    infoLink = models.URLField(null=True, blank=True)
    imgPath = models.FileField(upload_to='static/part_images', null=True, blank=True)
    price = models.PositiveIntegerField(default=0, null=True, blank=True)

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        for var in vars(self):
            if not var.startswith('_'):
                if self.__dict__[var] == '':
                    self.__dict__[var] = None
        super(MW_part, self).save(*args, **kwargs)

views.py&GT;     类MW_partsList(查看):

    def get(self, request, offset, limit):
        resultModel = {}
        partList = {}
        partItem = []

        for row in MW_part.objects.raw('SELECT * FROM parts_mw_part limit %s, %s' %(offset, limit)):
            part_rec = {}
            part_rec['name'] = row.name
            part_rec['price'] = row.price
            part_rec['code'] = row.code
            part_rec['manufacture'] = row.manufacture
            part_rec['spec'] = row.spec
            part_rec['infoLink'] = row.infoLink
            part_rec['imgPath'] = row.imgPath
            partItem.append(part_rec)

        partList['partList'] = partItem
        partList['partCount'] = MW_part.objects.count()
        resultModel['resultModel'] = partList
        resultModel['resultStatus'] = { 'success' : 'True', 'error' : '', 'message' : '' }

        return JsonResponse(resultModel)

回溯&GT;

Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response

  132.response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/views/generic/base.py" in view

  71.return self.dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch

  89.return handler(request, *args, **kwargs)
File "/Users/sangwonlee/PycharmProjects/MW_Service/parts/views.py" in get

  39.return JsonResponse(resultModel)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/http/response.py" in __init__

  535.data = json.dumps(data, cls=encoder)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py" in dumps

  237.**kw).encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py" in encode

  192.chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py" in iterencode

  250.return _iterencode(o, 0)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/serializers/json.py" in default

  112.return super(DjangoJSONEncoder, self).default(o)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py" in default

  173.raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /parts/list/0/10
Exception Value: <FieldFile: None> is not JSON serializable

请给我一些帮助或其他好的方法。

1 个答案:

答案 0 :(得分:0)

我现在已经修好了。所以现在我想分享一下。请参阅下文。

views.py&GT;

class MW_partsList(View):

    def get(self, request, offset, limit):
        resultModel = {}
        partList = {}
        partItem = []

        for row in MW_part.objects.raw('SELECT * FROM parts_mw_part LIMIT %s, %s' %(offset, limit)):
            part_rec = {}
            part_rec['name'] = row.name
            part_rec['price'] = row.price
            part_rec['code'] = row.code
            part_rec['manufacture'] = row.manufacture
            part_rec['spec'] = row.spec
            part_rec['infoLink'] = row.infoLink
            try:
                part_rec['imgPath'] = row.imgPath.url
            except:
                part_rec['imgPath'] = None
            partItem.append(part_rec)

        partList['partList'] = partItem
        partList['partCount'] = MW_part.objects.count()
        resultModel['resultModel'] = partList
        resultModel['resultStatus'] = { 'success' : 'True', 'error' : '', 'message' : '' }
        logger.debug("JSON Making up complete.")
        return JsonResponse(resultModel)