Skip to content

maxcoredev/django-rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django-serialize

Simple response serializer, automatically serizlizing all specified relations and fields, except listed in model's PRIVY_FIELDS = []

Quick start

  1. Installation:
$ git clone https://github.com/maxcoredev/django-rest.git
$ cd django-rest
$ pip install dist/django-rest-0.1.tar.gz
  1. Define models:
from rest.models import RestModel, models

class Article(RestModel):
    title = models.CharField(max_length=255)
    text = models.TextField()
    views_count = models.IntegerField()

    # Feilds that must not be serialized if not forced with objects.only() and objects.force()
    PRIVY_FIELDS = ['views_count']
  1. Use it in views:
from rest.response import Response
from .models import Article

def article_list(request):
    articles = Article.objects.all()
    return Response(articles)
  1. To get pretty view in browser and make it compatible with Django-debug-toolbar add DebugMiddleware:
MIDDLEWARE = [
    ...
    'rest.middleware.DebugMiddleware',
]

Examples

Return all fields, except PRIVY_FIELDS

Related objects (O2O, M2O) are just pks (default)

Related collections (O2M, M2M) are omitted:

articles = Article.objects.all()

Omitting fields:

articles = Article.objects.defer('title')

Only listed fields, even if they are in PRIVY_FIELDS:

articles = Article.objects.only('title')

The only non-standard method - force add PRIVY_FIELDS to be serialized:

articles = Article.objects.force('views_count')

Add full-fledged related objects (O2O, M2O):

articles = Article.objects.all().select_related('category')

Add related collections (O2M, M2M) (full-fledged):

articles = Article.objects.all().prefetch_related('tags')

About

Simple response serializer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages