How to make filtering with SerializerMethodField()?

I'm creating table that show objects of a model and I have a SerializerMethodField that shows a value from a different table with same transaction ID.

The problem is I'm using the serializers for the filtering table and chargeback is not working in there. How Can I make it filterable?

Simplifying the code, a have this model:

class PSerializer(serializers.ModelSerializer):
    ...
    chargeback = serializers.SerializerMethodField()

    def get_value(self, obj):
        ctransaction = CTransaction.objects.raw('SELECT * '
                                                   'FROM ctransaction '
                                                   'WHERE TRIM(mid)=TRIM(%s) '
                                                   'AND TRIM(unique_id)=TRIM(%s) '
                                                   'AND TRIM(num)=TRIM(%s) '
                                                   'AND rc_code IN (%s, %s, %s)',
                                                   [obj.mid, obj.unique_id, obj.num, '1', '1', '1'])
        if len(cs_transaction) > 0:
            return 'Yes'
        return 'No'

If I understand your question correctly, your problem is the method name (get_value). you should change it to get_chargeback

class PSerializer(serializers.ModelSerializer):
    ...
    chargeback = serializers.SerializerMethodField()

    def get_chargeback(self, obj):
        ctransaction = CTransaction.objects.raw('SELECT * '
                                                   'FROM ctransaction '
                                                   'WHERE TRIM(mid)=TRIM(%s) '
                                                   'AND TRIM(unique_id)=TRIM(%s) '
                                                   'AND TRIM(num)=TRIM(%s) '
                                                   'AND rc_code IN (%s, %s, %s)',
                                                   [obj.mid, obj.unique_id, obj.num, '1', '1', '1'])
        if len(cs_transaction) > 0:
            return 'Yes'
        return 'No'
Back to Top