我已经使用邮递员将数据转换为json格式。
我写了以下代码:
def customer_get_meals(request, restaurant_id):
meals = MealSerializer(
Meal.objects.filter(restaurant_id = restaurant_id).order_by("-id"),
many = True,
context = {"request": request}
).data
return JsonResponse({"meals: meals"})
这是网址
url(r'^restaurant/account/$', views.restaurant_account, name = 'restaurant-account'),
url(r'^restaurant/meal/$', views.restaurant_meal, name = 'restaurant-meal'),
url(r'^restaurant/meal/add/$', views.restaurant_add_meal, name = 'restaurant-add-meal'),
url(r'^restaurant/meal/edit/(?P<meal_id>\d+)/$', views.restaurant_edit_meal, name = 'restaurant-edit-meal'),
来自rest_framework导入序列化器 从foodtaskerapp.models导入Restaurant,Meal
MealSerializer类(serializers.ModelSerializer): 图片= serializers.SerializerMethodField()
def get_image(self, meal):
request = self.context.get('request')
image_url = meal.image.url
return request.build_absolute_uri(image_url)
class Meta:
model = Meal
fields = ("id", "name", "short_description", "image", "price")
毕竟,在这种情况下,我无法在Postman中获得JSON格式: 位于/ api / customer / meals / 2 /的TypeError 为了允许非字典对象被序列化,请将安全参数设置为False。