Search code examples
djangodjango-rest-frameworkdjango-rest-viewsets

Django Serializer defining initial object


I can't seem to figure out how to pass in an initial value to a serializer. I have a multitenant django site and I am trying to now get APIs setup. The client field exists but needs to be hidden and read only. I thought this worked like a form and a view in traditional django. I would normally pass in the get_initial in the view. I tried that first but it doesn't work. I think I need to get the value directly in the serializer but I can't seem to get it to work.

model:

class Location(ClientAwareModel):
    name = models.CharField(max_length=64, blank=True)
    address = models.CharField(max_length=64)
    address2 = models.CharField(max_length=64, blank=True)
    city = models.CharField(max_length=64)
    state = USStateField()
    zip_code = USZipCodeField()

class Client(models.Model):
    name = models.CharField(max_length=100)
    subdomain_prefix = models.CharField(max_length=100, unique=True)

    def __str__(self):
        return self.name

class ClientAwareModel(models.Model):
    client = models.ForeignKey(Client, on_delete=models.PROTECT)

    class Meta:
        abstract = True

def hostname_from_request(request):
    # split on `:` to remove port
    return request.get_host().split(':')[0].lower()

def client_from_request(request):
    hostname = hostname_from_request(request)
    subdomain_prefix = hostname.split('.')[0]
    return Client.objects.filter(subdomain_prefix=subdomain_prefix).first()

serializer (you can see all my failed attempts commented out:

class LocationSerializer(serializers.ModelSerializer):
    def get_client(self, obj):
        # return client_from_request(self.request)
        return client_from_request(self.context['request'])

    # client = serializers.SerializerMethodField('get_client')
    # client = serializers.SerializerMethodField()
    # client = serializers.Field(source='get_client', read_only=True)
    # client = serializers.ReadOnlyField(source='get_client')
    # client = serializers.PrimaryKeyRelatedField(read_only=True, default='get_client')
    client = serializers.PrimaryKeyRelatedField(read_only=True, source='get_client')
    # client = serializers.HiddenField(default=get_client(self))

    class Meta:
        model = Location
        fields = ['name', 'address', 'address2', 'city', 'state', 'zip_code', 'client']

viewset:

class LocationViewSet(viewsets.ModelViewSet):
    queryset = Location.objects.all()
    serializer_class = LocationSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        client = client_from_request(self.request)
        return super().get_queryset().filter(client=client)

you can see the different ways I tried to pass in the value but no matter what I do I get

IntegrityError at /locations/
null value in column "client_id" violates not-null constraint

Solution

  • One easy way to pass the client object to serializer is to pass it in perform_create method, something like:

    from rest_framework import serializers
    
    
    class LocationViewSet(viewsets.ModelViewSet):
        queryset = Location.objects.all()
        serializer_class = LocationSerializer
        permission_classes = [permissions.IsAuthenticated]
    
        def get_queryset(self):
            client = client_from_request(self.request)
            return super().get_queryset().filter(client=client)
    
        def perform_create(self, serializer):
            client = client_from_request(self.request)
            if not client:
                raise serializers.ValidationError("Client does not exist")
            serializer.save(client=client)
    

    And also remove the client field from your serializer:

    class LocationSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = Location
            fields = ['name', 'address', 'address2', 'city', 'state', 'zip_code']