Python Django RestFramework route trigger -
i'am building api using python 2.7 , django 1.7 , i'm facing problem. i'm not expert in rest framework understand basic mechanisms.
i can resume problem giving example. have route lets say
/api/project/
django rest framework provides me basic operations route , don't need write them e.g:
post, get, put, delete => /api/project/
the fact is, want operation when create new project. want add id of user has created new project.
i want add kind of trigger/callback create function:
class projectviewset(viewsets.modelviewset): queryset = project.objects.all() serializer_class = projectserializer def create(self, request, *args, **kwargs):
i want keep internal behavior of rest framework (i don't want rewrite route functions), want route stuff , need request object in trigger/callback. like
def callback(request, instance): instance.created_by = request.user instance.save()
do have ideas?
thank you.
you need add creator_id field serializer model represented resource. can in view:-
import copy class projectviewset(viewsets.modelviewset): ... def create(self, request, *args, **kwargs): data = copy.deepcopy(request.data) data['creator_id'] = request.user.id request._data = data return super(projectviewset, self).create(request, *args, **kwargs)
Comments
Post a Comment