[英]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.