订购选择字段

时间:2019-08-13 17:55:38

标签: python django django-rest-framework

我正在创建RESTFull Django API。 我已经创建了模型,序列化器和视图集。

Models.py:

class Payer(AddressAbstract, ContactAbstract, DateTimeDBOps, SoftDeleted):
    CPF = '1'
    CNPJ = '2'

    DU_TYPE_CHOICE = ((CPF, _("CPF")), (CNPJ, _("CNPJ")))

    DIGITACAO = 1
    BANCO = 2
    IMPORTACAO_ARQUIVO = 3
    IMPORTACAO_CSV = 4

    TYPE_ORIGIN = (
        (DIGITACAO, 'Digitação'),
        (BANCO, 'Banco'),
        (IMPORTACAO_ARQUIVO, 'Importação arquivo'),
        (IMPORTACAO_CSV, 'Importação CSV')
    )

    id = models.AutoField(
        verbose_name=_('Id'),
        primary_key=True,
        db_column='cd_pagador'
    )
    covenant = models.ForeignKey(
        Covenant,
        verbose_name=_('Covenant'),
        on_delete=models.CASCADE,
        db_column='cd_convenio',
        blank=True,
        null=True
    )
    name = models.CharField(
        verbose_name=_('Name'),
        max_length=40,
        db_column='nm_pagador'
    )
    type_origin = models.PositiveIntegerField(choices=TYPE_ORIGIN,
                                          default=DIGITACAO,
                                          db_column='cd_origem')
    ...

Viewset.py:

class PayerCreateListView(ListCreateAPIView):
    serializer_class = PayerListSerializer
    ordering_fields = ('name', 'created_at', 'type_origin', 'type_origin_display', 'id', 'covenant', 'du', 'du_type',
                   'dt_last_access', 'email', 'phone', 'address', 'neighborhood', 'city', 'uf', 'cep')
    filter_fields = ['covenant']
    filter_class = PayerFilter
    search_fields = ('name', 'du', 'covenant__code', 'covenant__bank__name', 'covenant__bank__id',
                 'covenant__company__name', 'covenant__company__du')

    def get_queryset(self):
        return Payer.objects.active()

    def get_serializer_class(self):
        if self.request.method == 'POST':
            return PayerCreateSerializer
        return PayerListSerializer

Serializers.py:

class PayerListSerializer(serializers.ModelSerializer):
    covenant = CovenantSerializer()
    du = serializers.SerializerMethodField('payer_du')
    type_origin_display = serializers.SerializerMethodField()

    class Meta:
        model = Payer
        fields = ('id', 'covenant', 'name', 'created_at', 'updated_at', 'du', 'du_type', 'email',
              'phone', 'address', 'neighborhood', 'city', 'uf', 'cep', 'type_origin', 'type_origin_display')

    def payer_du(self, obj):
        return format_du(obj.du_type, obj.du)

    def get_type_origin_display(self, obj):
        return '{}'.format(obj.get_type_origin_display())

现在,我想在视图集上排序这些字段(ordering_fields)。 但是,当我尝试对选择字段进行排序时,它是按其编号而不是“人类可读”进行排序的。

这里https://docs.djangoproject.com/en/2.2/ref/models/instances/#extra-instance-methods,说我可以用 get_FOO_display()读取“可读的内容”。 但是,我无法通过“人类可读的”命令对这些字段进行排序。 没用 如果我使用“ type_origin”(在ordering_fields上),它将按数字排序。

但是,如果我在前端使用'type_origin_display',它会以http 500中断: “无法将关键字'type_origin_display'解析为字段。选择包括:地址,cep,城市,公约,covenant_id,created_at,已删除,deleted_at,文档,dt_last_access,du,du_type,电子邮件,id,名称,邻居,电话, type_origin,uf,updated_at”

关于如何通过“人类可读”来排序选择字段的任何想法?

0 个答案:

没有答案