Fast DRF(Django REST Framework) is for making REST API development quicker/faster with awesomeness of Django. It will help you to write less code and develop API fastest as you can. Also you can override everthing(queryset, view class, serializer, etc) you want.
It has a plain and simple architecture. It brings your configuration and create a runtime serializer and a runtime viewset. So, there is no option to slow down your API. It's similar as native rest framework.
Procedure of execution
pip install fast-drf
settings.py
and add the following lines,FAST_DRF_CONFIG = {
'DEFAULT_APPLIED_APPS': (
'example_app', 'another_app'
)
}
urls.py
from fast_drf.router import BasicRouter
urlpatterns = BasicRouter.get_urls()
Write the following classmethod into your model. and don't forget to extend from ExposeApiModelMixin your model.
@classmethod
def exposed_api(cls, *args, **kwargs):
return {
'api_url': 'an-awesome-api'
}
Now you will get API like, /api/v1/an-awesome-api/
Suppose you have an API and your API using by so many client software. Now you found a bug or just need to remove some fields or add some fields. In that case your existing clients get interrupted. So, what you will do? Keep the old API and make a new one without extras hassle. Follow me,
@classmethod
def api_version_fields(cls, **kwargs):
return {
'v1': ['id', 'name', 'custom_1', 'custom_2'],
'v2': ['id', 'name', 'something_else']
}
Note You can pass list or tuple. But you must make sure that everthing you have passed into the each array that must me self attribute. For example, In this case something_else
must be a model property, fields. You can write into model like following,
@property
def something_else(self):
return 'This is a value'
Set APPEND_SLASH = True
at your settings.py
/api/
as a prefixSet you API prefix as your own like following. Just update into settings.py
FAST_DRF_CONFIG = {
# ...
'DEFAULT_API_PREFIX': 'rest-api' # Default 'api'
# ...
}
Your API will look like, /rest-api/v1/users/
from fast_drf.mixins.expose_api_model_mixin import ExposeApiModelMixin
from django.db import models
class MyModel(ExposeApiModelMixin, models.Model):
#... All yor fields
pass
# The following methods are available from model mixin
@classmethod
def exposed_api(cls, *args, **kwargs):
"""
This method holds a bunch of API configs and return like following...
{
"api_url": "", # (REQUIRED)
# You must use from HTTPVerbsEnum. Like HTTPVerbsEnum.GET.value, HTTPVerbsEnum.POST.value
"allowed_methods": ['get', 'post', 'put', 'patch', 'delete'], # (NOT REQUIRED)
# slug_field is application 'put', 'patch', 'delete' these methods
"slug_field": "pk", # (NOT REQUIRED) DEFAULT [PK] (Must be model field, unique or primary key)
"queryset": "", # (NOT REQUIRED) default all
"viewset_class": "", # (NOT REQUIRED) BaseViewset class
"serializer_class": "", # (NOT REQUIRED) default BaseEntitySerializer
"permission_classes": "", # (NOT REQUIRED) default set from settings
}
:param args:
:param kwargs:
:return: An empty Dictionary/False OR Full config dictionary.
"""
api_configs = {
"api_url": 'my-model-api',
}
return api_configs
That's it. You can also override serializer class and viewset class
Suppose you have a nice API like: api/v1/posts/
. I assume your model have fields like, title
, description
, author
(ForeignKey with User) etc. And you need to filter you data based on these fields. Where it comes default filtering enabled for all the model fields. You just need to pass the param into the API just like following...
http://yourdomain.com/api/v1/posts/?search=1&title:icontains=test&description:icontains=hello&author_id=10
You don't need to pass all the fields. Just pass the param you want to be filtered out. One thing to remember is search=1
. If you forget to put it, you won't be able to filter using all those params. So, search=1
is important here. Another good news is, here it supports all the django filtering options like, icontains
, contains
, exact
, iexact
etc. Remember whenever you are going to use one of these you must seperate the filtered field with :
. Like, title:icontains
. That's it 😃