Overview
A better way than DRF proposed for do some basic filters
Requirements
Python (2.7,3.2,3.3,3.4, 3.5 -with Django 1.8-)
Django (1.7, 1.8)
Django REST Framework (3.0, 3.1, 3.2)
Installation
Install using pip…
$ pip install drf-lafv
Example
Imagine that you have the following models.py
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=250)
nickname = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
publication_date = models.DateField()
isbn = models.CharField(max_length=17)
author = models.ForeignKey(Author)
And you want to filter your list of books by author’s nickname, isbn and create a custom filter which will allow to get you books that were published less than 30 days ago. Well, DRF per se, ask you to install django-filter and write a bunch of stuff that maybe hard. With lafv you can accomplish this doing this:
from lafv.views import ListAPIFilteredView
from serializers import BookSerializer
class BookView(ListAPIFilteredView):
queryset = Book.objects.all()
serializer_class = BookSerializer
filter_fields = ('author__nickname', 'isbn')
custom_filters = ('new', )
def filter_new(self, queryset):
"""
get books that publication date is no longer than 1 month
"""
last_month = datetime.today() - timedelta(days=30)
queryset = queryset.filter(publication_date__gte=last_month)
return queryset
And that’s it!, please check the examples on the test app
Testing
Install testing requirements.
$ pip install -r requirements.txt
Run with runtests.
$ ./runtests.py
You 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:
$ tox
Documentation
To build the documentation, you’ll need to install mkdocs.
$ pip install mkdocs
To preview the documentation:
$ mkdocs serve
Running at: http://127.0.0.1:8000/
To build the documentation:
$ mkdocs build
Release files for drf-lafv 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| drf-lafv-0.0.2.tar.gz | 3.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_lafv-0.0.2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 6.9 kB
Release files / drf-lafv-0.0.2.tar.gz
| Download URL | drf-lafv-0.0.2.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c5ab9e573860e77c2a74b5bb763e6b2669fb7c1cfae7b5f2dadf64e5eccc064c
|
|
BLAKE2b-256 checksum How to use checksums |
14be0e4095058e985e907cf0631f76297ebe2d0147e093dd0f796625650912ea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / drf_lafv-0.0.2-py2.py3-none-any.whl
| Download URL | drf_lafv-0.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 3.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
67f34bc4d5cdfce5fc9f36f2845af9475c329ea5b9ed711d15d4e095b840ed84
|
|
BLAKE2b-256 checksum How to use checksums |
591f74b7e15582123366bca7d5f0bedb132ee5f29652f12fa1164173d42709e3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |