Django REST Framework set related field by another field than PK (without NestedSerializer)

Viewed 541

I have the following serializer:

class OrderSerializer(serializers.ModelSerializer):
    pair_name = serializers.ReadOnlyField(source='pair.name', read_only=False)
    deposit_address = NestedReadOnlyAddressSerializer(many=False, read_only=True)
    withdraw_address = NestedAddressSerializer(many=False, read_only=False, partial=True)

pair.name is a unique field.

Is it possible to allow the PK assignment (order.pair) using the pair name.

[POST] {'withdraw_address': 'x', deposit: 'address': 'y', 'pair_name': 'ETHBTC'}

Instead of:

[POST] {'withdraw_address': 'x', deposit: 'address': 'y', 'pair': 1}

Tried looking thought the docs and did not find a solution. Googling for an hour did not help as well.

Currently lookup_field can only be set on ViewSet and not on Serializer.

1 Answers
Related