方法对象不是JSON可序列化的

时间:2017-12-28 13:14:07

标签: python django python-3.x django-views django-serializer

我删除购物车项目时使用ajax刷新购物车项目。它工作得很好,如果我不用图像响应对象,否则我会收到错误method object is not JSON serializable。如果我对图像部分使用model_to_dict,则会收到错误'function' object has no attribute '_meta'

这是代码

def cart_detail_api_view(request):
    cart_obj, new_obj = Cart.objects.new_or_get(request)
    products = [{
            "id": x.id,
            "url": x.get_absolute_url(),
            "name": x.name,
            "price": x.price,
            "image": x.first_image
            }
            for x in cart_obj.furnitures.all()]
    cart_data  = {"products": products, "subtotal": cart_obj.sub_total, "total": cart_obj.total}
    return JsonResponse(cart_data)

class Furniture(models.Model):
    name = models.CharField(max_length=100, blank=True, null=True)
    manufacturer = models.ForeignKey(Manufacturer, blank=True, null=True)
    slug = models.SlugField(max_length=200, unique=True)

    def __str__(self):
        return self.name

    def first_image(self):
        """
        Return first image of the furniture otherwise default image
        """
        if self.furniture_pics:
            return self.furniture_pics.first()
        return '/static/img/4niture.jpg'

class Cart(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    furnitures = models.ManyToManyField(Furniture, blank=True)

'function' object has no attribute '_meta'包裹到x.first_image

时出错model_to_dict

我如何解决此类问题?

已更新

class FurniturePic(models.Model):
    """
    Represents furniture picture
    """
    furniture = models.ForeignKey(Furniture, related_name='furniture_pics')
    url = models.ImageField(upload_to=upload_image_path)

1 个答案:

答案 0 :(得分:6)

如您所知,问题出在:

"image": x.first_image

first_image是一个函数,因此无法转换为JSON。您要做的是序列化first_image返回的值。因此,为此,您需要调用此功能:

"image": x.first_image() # note the brackets

此外,我还注意到另一个问题:

return self.furniture_pics.first() # will return the image object; will cause error

因此,您必须将其更改为:

return self.furniture_pics.first().url # will return the url of the image

<强>更新

self.furniture_pics.first().url将返回FurniturePic.url ImageField。您需要该图片的网址进行序列化。你必须这样做:

return self.furniture_pics.first().url.url # call url of `url`

正如您所看到的,这让人感到困惑。我建议将FurniturePic.url字段的名称更改为FurniturePic.image。但是,请随意忽略它。