Django date view archive for rest framework
- Python (3.6)
- Django REST Framework (3.8)
Install using pip
…
$ pip install djangorestframework-date-archive
To enable date archive in your project, rest_framework_date_archive
must be added to your INSTALLED_APPS.
rest_framework_date_archive provides your drf model viewsets with a read-only date archive similar to that of django.
The archive can be accessed through the following urls:
items\archive\year\
items\archive\year\month\
items\archive\year\month\day\
Setting things up is pretty easy:
Your model manager must include a queryset that inherit from DateArchiveMixin
.
The name of the field with which the data is archived is specified by the class attribute archive_field
:
class BlogQueryset(DateArchiveMixin,
models.QuerySet):
archive_field = 'date'
class Blog(models.Model):
date = models.DateField()
content = models.TextField(default='')
objects = BlogQueryset.as_manager()
Your model viewset must inherit from DateArchiveView
:
class BlogViewSet(DateArchiveView,
ModelViewSet):
model = Blog
queryset = Blog.objects.all()
serializer_class = BlogSerializer
And you must register your urls with the DateArchiveRouter
:
router = DateArchiveRouter()
router.register('blogs', BlogViewSet)
Once these few steps are performed, you have an easy way to access archived data through your REST Api. For example for blogs archives of October 2018, you can simply call: blogs\archive\2018\10\
.
Install testing requirements.
$ pip install -r requirements.txt
You can 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:
$ tox