how can I save created_by
as request user? Note that this field is not defined in the serializer
class User(models.Model):
email = models.EmailField(...)
name = models.CharField(...)
created_by = models.ForeignKey('User')
class UserSerializer(serializer.ModelSerializer):
class Meta:
model = User
fields = ['email', 'name']
class CreateUserApiView(CreateAPIView):
model = User
serializer_class = CreateRequesterSerializer
def create(self, request, *args, **kwargs):
# how can I save `created_by` as request user?
return super(CreateUserApiView, self).create(request, *args, **kwargs)
Override the perform_create()
method of CreateAPIView
class CreateUserApiView(CreateAPIView):
# your code
def perform_create(self, serializer):
serializer.save(created_by=self.request.user)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With