drf-tracking provides a Django model and DRF view mixin that work together to log Django Rest Framework requests to the database. You'll get these attributes for every request/response cycle to a view that uses the mixin:
| Model field name | Description | Model field type |
|---|---|---|
user | User if authenticated, None if not | Foreign Key |
requested_at | Date-time that the request was made | DateTimeField |
response_ms | Number of milliseconds spent in view code | PositiveIntegerField |
path | Target URI of the request, e.g., "/api/" | CharField |
remote_addr | IP address where the request originated (X_FORWARDED_FOR if available, REMOTE_ADDR if not), e.g., "127.0.0.1" | GenericIPAddressField |
host | Originating host of the request, e.g., "example.com" | URLField |
method | HTTP method, e.g., "GET" | CharField |
query_params | Dictionary of request query parameters, as text | TextField |
data | Dictionary of POST data (JSON or form), as text | TextField |
response | JSON response data | TextField |
status_code | HTTP status code, e.g., 200 or 404 | PositiveIntegerField |
- Python 2.7
- Django (1.7, 1.8. 1.9)
- Django REST Framework (3.0, 3.1, 3.2, 3.3)
Install using pip...
$ pip install drf-trackingRegister with your Django project by adding rest_framework_tracking to the INSTALLED_APPS list in your project's settings.py file. Then run the migrations for the APIRequestLog model:
$ python manage.py migrateAdd the rest_framework_tracking.mixins.LoggingMixin to any DRF view to create an instance of APIRequestLog every time the view is called.
For instance:
# views.pyfromrest_frameworkimportgenericsfromrest_framework_tracking.mixinsimportLoggingMixinclassLoggingView(LoggingMixin, generics.GenericAPIView): defget(self, request): returnResponse('with logging')For performance enhancement, explicitly choose methods to be logged using logging_methods attribute:
classLoggingView(LoggingMixin, generics.CreateModelMixin, generics.GenericAPIView): logging_methods= ['POST', 'PUT'] model= ...Install testing requirements.
$ pip install -r requirements.txtRun with runtests.
$ ./runtests.pyYou can also use the excellent tox testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run:
$ toxTo build the documentation, you'll need to install mkdocs.
$ pip install mkdocsTo preview the documentation:
$ mkdocs serve Running at: http://127.0.0.1:8000/To build the documentation:
$ mkdocs build