Now edited for REST framework 3
With REST framework 3 the pattern is now:
if serializer.is_valid():
serializer.save(user_id=15)
Note that the serializers do not now ever expose an unsaved object instance as serializer.object
, however you can inspect the raw validated data as serializer.validated_data
.
If you're using the generic views and you want to modify the save behavior you can use the perform_create
and/or perform_update
hooks...
def perform_create(self, serializer):
serializer.save(user_id=15)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…