python - 如何在 Django 休息框架响应中获取过滤的 Django ORM 查询结果

标签 python mysql django django-models django-rest-framework

我的问题是关于在 Django-rest-framework api 的响应中获取 Django 查询结果。 我的 Django 项目中有以下产品、属性和产品选项模型:

class Product(models.Model):
    productid = models.AutoField(db_column='productId', primary_key=True)  
    productname = models.CharField(db_column='productName', max_length=200)

class Attribute(models.Model):
    attributeid = models.AutoField(db_column='attributeId', primary_key=True)
    attributename = models.CharField(db_column='attributeName', max_length=200, blank=True, null=True) 

class Productoptions(models.Model):
    optionsid = models.AutoField(db_column='OptionsId', primary_key=True)
    optionproductid = models.ForeignKey(Product, models.DO_NOTHING, db_column='optionProductId', blank=True, null=True)
    optionattributeid = models.ForeignKey(Attribute, models.DO_NOTHING, db_column='optionAttributeId', blank=True, null=True)

我在所有三个表中都填充了示例数据,并且在尝试获取属性名称为 nike 的产品时,以下 Django 查询在 Python shell 中完美运行。

Productoptions.objects.filter(optionattributeid__attributename='nike').values('optionproductid__productname')

得到结果

<QuerySet [{'optionproductid__productname': 'nike shirt'}, {'optionproductid__productname': 'nike tracksuit'}]>

但是相关的模型类查询在我看来不起作用

class ProductOptionsView(APIView):
    serializer_class = ProductOptionsSerializer

    def get(self,request):
        queryset = Productoptions.objects.filter(optionattributeid__attributename='nike').values('optionproductid__productname')

        serializer = self.serializer_class(queryset, many=True)
        return Response(serializer.data)

我没有从这个 View 中得到想要的结果。有什么方法可以让所有 Django 查询都简单地提供 Django-rest-framework 的结果。所有 get 和 post 查询都在 Python shell 中完美运行。

是否有任何其他方式或 DRF 有自己的方法来使用嵌套模型导致 rest api。我也读过 Django Filter 但它在 Django 的网站上缺少文档。

最佳答案

要补充@CrowbarKZ 在评论中所说的内容,首先删除查询集末尾的 .values,序列化程序的输入需要是查询集,而不是字典。如果您想在 ProductoptionSerializer 中包含 ProductAttribute 字段,您也需要这些序列化器,如文档中所述:http://www.django-rest-framework.org/api-guide/serializers/#dealing-with-nested-objects

class ProductSerializer(serializers.Serializer):
    class Meta:
        model = Product
        fields = (productid, productname)

class AttributeSerializer(serializers.Serializer):
    class Meta:
        model = Attribute          
        fields = (attributeid, attributename)

class ProductoptionSerializer(serializers.Serializer):
    optionproductid = ProductSerializer()
    optionattributeid = AttributeSerializer()
    optionsid = serializers.IntegerField()

    class Meta:        
        model = Productoption
        fields = (optionproductid, optionattributeid, optionsid)

关于python - 如何在 Django 休息框架响应中获取过滤的 Django ORM 查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48623389/

相关文章:

python - 替换 pandas 数据框列中的一些字符串值,其值应为浮点型

mysql - 使用变量查询.Sql文件

django - 为什么模型 ID 不总是 Postgres/Django 中的下一个递增数字?

python - 如何提交包含多个对象的表单 - django

python - 我如何告诉 POpen 使用/设置某些环境变量?

python - 适用于 Python 的 Google API 客户端。批量请求: how to access to a specific request in the callback

python - 使用 Python 取消汇总数据集

python - Python xml.dom 中的非递归(单节点级别)getElementsByTagName

mysql - 有没有办法将 header 添加到 mysql 查询结果中?

mysql - 如何解决mysql中的时间数据类型