V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Kaco
V2EX  ›  Python

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

  •  
  •   Kaco · 2019-09-14 21:53:04 +08:00 · 2711 次点击
    这是一个创建于 1925 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下

    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 会报错

    10 条回复    2019-09-15 20:56:55 +08:00
    Kaco
        1
    Kaco  
    OP
       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
        2
    imzxk  
       2019-09-14 22:24:37 +08:00
    class CategoryApiSerializer(serializers.ModelSerializer):
    foods = serializers.SerializerMethodField()
    imzxk
        3
    imzxk  
       2019-09-14 22:27:08 +08:00
    class CategoryApiSerializer(serializers.ModelSerializer):
    foods = FoodApiSerializer(many=True) def to_representation(self, instance): #这里过滤试试
    Kaco
        4
    Kaco  
    OP
       2019-09-14 22:37:56 +08:00
    @imzxk 唔 还是不行....
    Kaco
        5
    Kaco  
    OP
       2019-09-14 22:39:08 +08:00
    @imzxk 而且 to_representation 还没用过,我是把之前过滤的代码拉下来用的 不知道对不对
    windfarer
        6
    windfarer  
       2019-09-14 23:20:32 +08:00
    for 里面 return 了可不就出来一个东西就结束了
    Kaco
        7
    Kaco  
    OP
       2019-09-14 23:22:12 +08:00
    @windfarer return 也只能返回一个数据,就会中断了
    Kaco
        8
    Kaco  
    OP
       2019-09-14 23:24:35 +08:00
    @Kaco FoodApiSerializer 是接收不了一个多个 queryset 对象, 只能接收一个,其他被无视
    Kaco
        9
    Kaco  
    OP
       2019-09-14 23:26:32 +08:00
    已经解决...是我漏了一个参数
    return FoodApiSerializer(foods,many=True, context={"request": self.context}).data
    加上 many=True 就可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2699 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 10:06 · PVG 18:06 · LAX 02:06 · JFK 05:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.