장바구니 항목을 제거 할 때 장바구니 항목을 새로 고치는 데 아약스를 사용하고 있습니다. 그것은 잘 작동합니다. 그렇지 않으면 이미지가있는 객체로 응답하지 않으면 에러가 발생합니다. method object is not JSON serializable
. 이미지 부분에 model_to_dict
을 사용하면 'function' object has no attribute '_meta'
오류가 발생합니다. 여기메서드 개체가 JSON 직렬 가능하지 않습니다
내가 같은 문제를 해결하려면 어떻게 model_to_dict
에 x.first_image
을 포장하는 동안이 '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)
입니까?
"image": x.first_image
first_image
가 함수 인, 그래서 JSON으로 변환 할 수 없습니다에
은 여러분이 알다시피,
class FurniturePic(models.Model):
"""
Represents furniture picture
"""
furniture = models.ForeignKey(Furniture, related_name='furniture_pics')
url = models.ImageField(upload_to=upload_image_path)
그런 식으로 'Object of type'의 FurniturePic '은 JSON을 직렬화 할 수 없습니다.' 하나의 질문에 다른 종류의 에러를 보여 주려고 주저했다. – pri
또한 x.first_image()를 시도했다. 'Object of type'ImageFieldFile '이 JSON을 직렬화 할 수 없다.'이 오류 – pri
은'self.furniture_pics가 아닙니다. first(). url''x.first_image(). url'와 동일합니까? – pri