简体   繁体   English

如何将序列化程序中的字段获取到 ListView 中进行过滤

[英]How to get fields from Serializer into ListView for filtering

the model in question:有问题的 model:

class CustomerPrices(models.Model):
    url = models.OneToOneField('CustomerProductUrls', models.DO_NOTHING, db_column="url", primary_key=True)
    written = models.DateTimeField()
    reseller = models.CharField(max_length=250)
    price = models.FloatField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'customer_prices'
        unique_together = (('url', 'written', 'reseller'),)

the serializer (and one related serializer) in question:有问题的序列化程序(和一个相关的序列化程序):

class CustomerProductUrlsSerializer(serializers.ModelSerializer):
    ean = CustomerProductsSerializer()
    url = serializers.CharField(max_length=255, required=False)
    class Meta:
        model = CustomerProductUrls
        fields = '__all__'

class CustomerPricesSerializer(serializers.ModelSerializer):
    written = serializers.DateTimeField(format='%Y-%m-%d %H:%M:00', required=False)
    reseller = serializers.CharField(max_length=250, required=False)
    url = CustomerProductUrlsSerializer()
    name = serializers.SerializerMethodField('get_name')
    ean = serializers.SerializerMethodField('get_ean')
    url = serializers.SerializerMethodField('get_url')
    price = serializers.FloatField()

    class Meta:
        model = CustomerPrices
        fields = '__all__'

    def get_name(self, obj):
        return obj.url.ean.name

    def get_ean(self, obj):
        return obj.url.ean.ean

    def get_url(self, obj):
        return obj.url.url

and the ListAPIView for the CustomerPrices class:以及 CustomerPrices class 的 ListAPIView:

class CustomerPricesListView(generics.ListAPIView):
    serializer_class = CustomerPricesSerializer
    filter_backends = (DjangoFilterBackend, OrderingFilter)
    fields = ('written', 'url', 'price')
    filter_fields = fields
    search_fields = fields
    def get_queryset(self):
        """
        This view should return a list of all the products where price >= 70
        """
        return CustomerPrices.objects.filter(price__gte=70)

Inside my CustomerPricesSerializer I've got a field named ean as well as name the values for these two come through the related CustomerProductUrlsSerializer (and corresponding model).在我的CustomerPricesSerializer中,我有一个名为ean的字段,并通过相关的CustomerProductUrlsSerializer (和相应的模型) name这两个字段的值。 The code is working so far, I get a response looking like this:到目前为止,该代码正在运行,我得到如下响应:

"results": [
    {
        "url": "https://www.example.com/p/12345678",
        "written": "2020-04-05 12:00:00",
        "reseller": "ABC123",
        "name": "Product 12345678",
        "ean": "1234567890123",
        "price": 98.3
    }, ... 
    ]

I'm using the DjangoFilterBackend and I would like to filter on the ean as well as the name , which is available in my response.我正在使用DjangoFilterBackend ,我想过滤ean以及name ,这在我的响应中可用。 But as soon as I add ean to my fields tupel inside the Serializer I get the Meta.fields contains fields that are not defined on this FilterSet: ean .但是,一旦我将ean添加到序列化程序内的fields元组中,我就会得到Meta.fields contains fields that are not defined on this FilterSet: ean I do understand that my queryset is returning the fields from CustomerPrices Model inside my ListAPIView, but how do I get get the ean as well as the name to be a part of the queryset and therefore a part of the available fields我确实知道我的查询集在我的queryset中返回来自CustomerPrices Model 的字段,但是我如何让ean以及name成为查询集的一部分,从而成为可用fields的一部分

The field ean does not belong to the CustomerPrices model but somewhere else.字段ean不属于CustomerPrices model 而是其他地方。 The DRF expects the filter tuples are directly related to the model (here CustomerPrices ) and hence the error. DRF 预计过滤器元组与 model (此处为CustomerPrices )直接相关,因此错误。

To resolve this issue, you have to provide the actual relationship to the ean field.要解决此问题,您必须提供与ean字段的实际关系

class CustomerPricesListView(generics.ListAPIView):
    fields = ('written', 'url', 'price', 'url__ean__ean', 'url__ean__name')
    filter_fields = fields
    # rest of the code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么方法可以将一些特定字段从序列化程序获取到另一个序列化程序? - Is there any way to get some specific fields from a serializer to another serializer? 如何从返回所有字段(字段 = '__all__' in serializer's meta)的 django 模型序列化器中获取特定字段? - How to get specific fields from a django model serializer which returns all field(fields = '__all__' in serializer's meta)? 如何将一些 UserModel 字段放入另一个 Serializer - How to get some of the UserModel fields into another Serializer 如何从 DRF 中的 Serializer 中提取模型字段? - How to extract model fields from Serializer in DRF? 如何在序列化器中从父对象模型获取字段-Django Rest Framework - How to get fields from parent object model in serializer - django rest framework 如何通过 DRF 序列化程序获取必填字段作为 json 列表 - How to get required fields as a list of json by DRF serializer 如何在APIView中获取序列化器字段? - How can I get the serializer-fields in APIView? 如何从Django中的序列化器获取实例 - How to get an instance from a serializer in Django 如何从序列化程序中获取用户? - How can I get user from serializer? 如何根据条件从序列化程序中添加或删除字段? - How to add or remove fields from a serializer based on a condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM