首页 > 解决方案 > 如何在同一页面上传递两个查询集?(django rest框架和react)

问题描述

我想在同一个 url 中显示两个查询集值。我有我的主页,我在其中传递所有产品列表,在同一页面上的该部分之后,我试图根据类别显示产品,例如第一个最近的产品,然后是下一部分所有自行车(基于类别)。

@api_view(['GET'])

def getProducts(request):

     products = Product.objects.all()

     p = Product.objects.all().filter(category = 1)


   context = {
        "request": request,
    }
    
first_serializer = ProductSerializer(products, many= True, context=context)
second_serializer = ProductSerializer(p, many= True, context=context)

response = first_serializer.data + second_serializer.data
return Response(response)

这是我的反应页面

最近的产品



        {loading ? <Loader />
            : error ? <Message variant='danger'>{error}</Message>
                :
                <Row>
                    {products.map(product => (
                        <Col key={product._id} sm={12} md={6} lg={4} xl={3}>
                            <Product product={product} />
                        </Col>
                    ))}
                </Row>     
            }

我想要这样的相同程序

 <Row>
                    {p.map((product) => (
                        <Col key={product._id} sm={12} md={6} lg={4} xl={3}>
                            <Product product={product} />
                        </Col>
                    ))}
                </Row>  

            
             <Row>

但我无法映射 p 对象。我现在该怎么办?我正在使用 redux 的反应。

标签: reactjsdjangodjango-rest-frameworkreact-redux

解决方案


推荐阅读