I have used tastypie in django to handle REST api request. When ever I do GET/POST request it comes to dehydrate method by default. But for DELETE/PUT request it doesn't have any method tastypie handles it, so there is no way to log request information.
class ProjectResource(ModelResource):
allowed_methods = ['get', 'put', 'post', 'delete']
resource_name = 'project'
queryset = Project.objects.all()
validation = FormValidation(form_class=ProjectForm)
always_return_data = True
filtering = {
'id': ALL,
'slug': ALL,
}
def dehydrate(self, bundle):
import pdb;pdb.set_trace() #--> get/post request hit this function
logger.log('app.main','debug', 'Project info', bundle)
bundle.data['name'] = cgi.escape(bundle.obj.name)
return bundle
Is there any function to override ModelResource in tastypie.resource module? so that for all request i get the bundle data first and then i will pass that to logger before it is processed.
A bit late, but if you can describe what the purpose is, it might be easier to point you in the right direction.
You say you would like to log request information, but logging the bundle in the dehydrate
method doesn't do that, as any changes such as adding the primary key on a POST
request are already done.
You might be better off logging the request.body
directly in the dispatch
method.
If you actually would like to log the response you can also do that in the dispatch
method.
def dispatch(self, request_type, request, **kwargs):
logger.info('Project info [request method]: %s' % request.method)
logger.info('Project info [request]: %s' % request.body)
response = super(ClassName, self).dispatch(request_type, request, **kwargs)
logger.info('Project info [response status]: %d' % response.status_code)
logger.info('Project info [response]: %s' % response.content)
return response
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.