请教下, django restframework 做嵌套 JSON,但是二级 JSON 做了 ORM 筛选后显示不全

2019-09-14 21:53:04 +08:00
 Kaco

代码如下

class FoodApiSerializer(serializers.ModelSerializer): image_url = serializers.SerializerMethodField()

class Meta:
    model = Food
    fields = ('foodid', 'name', 'info', 'price', 'stock', 'image_url', 'num', 'status')

# imagefield 序列化以获取完整的 URL
def get_image_url(self, Food):
    request = self.context.get('request')
    request = request['request']
    image_url = Food.image.url
    return request.build_absolute_uri(image_url)

class CategoryApiSerializer(serializers.ModelSerializer):

foods = serializers.SerializerMethodField()

def get_foods(self, obj):
    foods = Food.objects.filter(Q(category=obj.categoryid), Q(status='True'))

    if foods is not None and len(foods) > 0:
        for x in foods:
            return FoodApiSerializer(x, context={"request": self.context}).data
    else:
        return ''

class Meta:
    model = Category
    fields = ('categoryid', 'categoryname', 'foods')
    read_only_fields = ('categoryid', 'categoryname', 'foods')

def create(self, validated_data):
    pass

def update(self, instance, validated_data):
    pass

现在 foods 下面的 json,只能显示一条数据,其他都没有 而且只能用 for 来 return FoodApiSerializer(x, context={"request": self.context}).data 或者 FoodApiSerializer(foods[0], context={"request": self.context}).data return FoodApiSerializer(foods, context={"request": self.context}).data 会报错

2683 次点击
所在节点    Python
10 条回复
Kaco
2019-09-14 21:58:04 +08:00
[
{
"categoryid": 1,
"categoryname": "特色菜",
"foods": {
"foodid": 1,
"name": "西蓝花炒牛肉 Tr",
"info": "西蓝花炒牛肉",
"price": 13.99,
"stock": 99999,
"image_url": "http://0.0.0.0:8000/media/food/2019/09/Recedetangle_21_Copy_8.png",
"num": 0,
"status": "True"
}
},
{
"categoryid": 2,
"categoryname": "小炒",
"foods": ""
},
{
"categoryid": 3,
"categoryname": "粥粉面",
"foods": ""
},
{
"categoryid": 4,
"categoryname": "汤品",
"foods": {
"foodid": 3,
"name": "人参汤 Tr",
"info": "人参汤",
"price": 9.82,
"stock": 22222,
"image_url": "http://0.0.0.0:8000/media/food/2019/09/Rectangle_21_Copy_8.png",
"num": 0,
"status": "True"
}
},
{
"categoryid": 5,
"categoryname": "围餐",
"foods": ""
}
]

每个 foods 分类下只显示一个菜品,其他都没显示
imzxk
2019-09-14 22:24:37 +08:00
class CategoryApiSerializer(serializers.ModelSerializer):
foods = serializers.SerializerMethodField()
imzxk
2019-09-14 22:27:08 +08:00
class CategoryApiSerializer(serializers.ModelSerializer):
foods = FoodApiSerializer(many=True) def to_representation(self, instance): #这里过滤试试
Kaco
2019-09-14 22:37:56 +08:00
@imzxk 唔 还是不行....
Kaco
2019-09-14 22:39:08 +08:00
@imzxk 而且 to_representation 还没用过,我是把之前过滤的代码拉下来用的 不知道对不对
windfarer
2019-09-14 23:20:32 +08:00
for 里面 return 了可不就出来一个东西就结束了
Kaco
2019-09-14 23:22:12 +08:00
@windfarer return 也只能返回一个数据,就会中断了
Kaco
2019-09-14 23:24:35 +08:00
@Kaco FoodApiSerializer 是接收不了一个多个 queryset 对象, 只能接收一个,其他被无视
Kaco
2019-09-14 23:26:32 +08:00
已经解决...是我漏了一个参数
return FoodApiSerializer(foods,many=True, context={"request": self.context}).data
加上 many=True 就可以了
sazima
2019-09-15 20:56:55 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/600841

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX