I am having an issue in setting the user automatically in django rest framework. In my models.py I have the following:
Models.py
class Space(models.Model):
creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
Serializers.py
class SpaceSerializer(serializers.HyperlinkedModelSerializer):
creator = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(serializers.CurrentUserDefault()))
class Meta:
model = Space
fields = '__all__'
The intent is that the creator field will be set during creation through the API. When I issue a POST request to create the model, i get the following error:
"IntegrityError at /api/space/ NOT NULL constraint failed: apiapp_space.creator_id
I assume the serializer is not even using this field override, as when I set null=True in my model, it seems to save correctly with the creator field set to null.
Note that I have also tried using serializers.ModelSerializer.
How can I get django to recognize this field override?