Q:

DRF Serializer foreign key reverse lookup

I'm trying to send the following HTTP Post API request to create a new EventInterest object. How can I accomplish this in a smallest payload instead of sending the entire object? I'm attempting an extra layer of security-through-obfuscation and instead of using the default integer pk, how can I use uuid for Event and username for User? .... Or do the extra SQL lookups negate the benefits of simplifying the payload and I should just use pk?
models.py
class Event(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField(max_length=500)
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, db_index=True, blank=True)

class EventInterest(models.Model):
    event = models.ForeignKey(Event)
    sender = models.ForeignKey(settings.AUTH_USER_MODEL)  # from User

api.py
class EventInterestViewSet(mixins.CreateModelMixin, GenericViewSet):

    queryset = models.EventInterest.objects.all()
    serializer_class = serializers.EventInterestSerializer
    lookup_field = 'uuid'

serializer.py
class EventInterestSerializer(serializers.ModelSerializer):
    # event = serializers.SlugRelatedField(read_only=True, slug_field='uuid')
    # recipient = serializers.SlugRelatedField(read_only=True, slug_field='username')
    # sender = serializers.SlugRelatedField(read_only=True, slug_field='username')

    class Meta:
        model = models.EventInterest
        fields = (
            'event',  #works with pk, want uuid
            'sender',  # works with pk, want username
        )

HTTP Post:
{
    "event": "da9290c6-f6f8-4d27-bfe0-d388ed911fe8",
    "sender":"eX8gkxJNDREv"  //this is the username field
}

A:

As you defined uuid in Event model, you have to define uuid in User model too. In order to do that, you have to extend the default user model. Then you have to override the create() method of EventInterestSerializer to do a lookup on respective UUID field instead of pk
models.py
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, db_index=True)

class Event(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField(max_length=500)
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, db_index=True)

class EventInterest(models.Model):
    event = models.ForeignKey(Event)
    sender = models.ForeignKey(User)

serializer.py
class EventInterestSerializer(serializers.ModelSerializer):
    class Meta:
        model = EventInterest
        fields = ('event', 'sender',)

    def create(self, validated_data):
        try:
            return EventInterest.objects.get(event__uuid=validated_data['event'],
                                             sender__uuid=validated_data['sender'])
        except EventInterest.DoesNotExist:
            raise serializers.ValidationError("No matching data found")

