gpt4 book ai didi

python - Django 休息框架 : Order by Serializer Method Field

转载 作者:行者123 更新时间:2023-12-02 16:40:00 24 4
gpt4 key购买 nike

我目前正在使用 Django REST framework 3.11.0 和 Django 3.0.6。我对 DRF 还是很陌生,我不确定如何解决这个问题。我正在尝试将排序应用于 SerializerMethodField 和枚举。我找到了一些 documentation这表明我可以制作自己的 OrderFilter,但我不知道如何制作。有没有我可以使用的例子?这是我的代码。

查看

from rest_framework import generics
from V1.assets.models.asset_main import Asset
from V1.assets.serializers.asset_serializer import AssetSerializer
from rest_framework import filters
from django_filters.rest_framework import DjangoFilterBackend

class AssetsView(generics.ListCreateAPIView):
queryset = Asset.objects.all()
serializer_class = AssetSerializer
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
filter_fields = ['asset_type']
search_fields = ['asset_type', 'asset_properties', 'current_employee_name']

模型

class AssetType(models.TextChoices):
LAPTOP = 'LPT', _('Laptop')
MOUSE = 'MSE', _('Mouse')
MONITOR = 'MTR', _('Monitor')
AC_ADAPTER = 'ADR', _('AC Adapter')
TELEPHONE = 'TLP', _('Telephone')
LOCK = 'LCK', _('Lock')
HEADSET = 'HDS', _('Headset')

class Asset(CreatedModified):

asset_type = models.CharField(
max_length=3,
choices=AssetType.choices,
default=None
)
asset_properties = JSONField(default=dict)
current_employee = models.ForeignKey(
Employee,
related_name='assets_current_employees',
related_query_name='assets_current_employee',
default=None,
on_delete=models.CASCADE,
null=True,
blank=True
)

class Meta:
app_label = 'assets'
default_related_name = 'assets'

def __str__(self):
return self.asset_type

序列化器

from V1.general.enums import AssetStatus, AssetType
from V1.accounts.models.employee_main import Employee

class AssetSerializer(serializers.ModelSerializer):

current_employee_name = serializers.SerializerMethodField('get_full_name_from_employee')
asset_type = serializers.CharField(source='get_asset_type_display')

class Meta:
model = Asset
fields = (
'id',
'asset_type',
'asset_properties',
'current_employee',
'current_employee_name'
)
extra_kwargs = {
'asset_type': {
'required': True,
'allow_blank': False
}
}

def get_full_name_from_employee(self, asset):
current_employee_name = asset.current_employee.first_name + ' ' + asset.current_employee.last_name
return current_employee_name

我似乎无法按 current_employee_nameasset_type 进行订购。我应该怎么做才能允许对这两个字段进行排序?

最佳答案

据我从您的问题中了解到,您想按当前员工姓名订购 Assets 。让我们以几个名字为例:

  • Jean d'Artagnan(名字:Jean,姓氏:d'Artagnan)
  • Joe 水管工(名字:Joe,姓:水管工)
  • 乔纳斯迈耶(名字:乔纳斯,姓氏:迈耶)

如果我们先按 first_name 排序,然后按 last_name 排序,则可以实现相同的顺序。如果有多个条目具有相同的 first_name,则将按 last_name 进行排序。

要实现这一点,您只需在 View 中指定 ordering_fields:

class AssetsView(generics.ListCreateAPIView):
filters = [filters.OrderingFilter]
ordering_fields = ['current_employee.first_name', 'current_employee.last_name']

关于python - Django 休息框架 : Order by Serializer Method Field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61885622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com