Associating Snippets with Users
Right now, if we created a code snippet, there'd be no way of associating the user that created the snippet, with the snippet instance. The user isn't sent as part of the serialized representation, but is instead a property of the incoming request.
The way we deal with that is by overriding a .perform_create()
method on our snippet views, that allows us to modify how the instance save is managed, and handle any information that is implicit in the incoming request or requested URL.
On the SnippetList
view class, add the following method:
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
The create()
method of our serializer will now be passed an additional 'owner'
field, along with the validated data from the request.
当前内容版权归 Django REST 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Django REST .