Skip to content

Files

Latest commit

 

History

History
155 lines (139 loc) · 2.7 KB

serialization.md

File metadata and controls

155 lines (139 loc) · 2.7 KB

Serialization

Object Response Attribute
HttpResponse HTTP-Response -
None HTTP-response -
int HTTP-response status code
bool JSON-response content
str HTTP-response content
str ending with '.html' HTTP-response template name
float HTTP-response content
dict JSON-response content
list JSON-response content
Geometry GeoJSON-response content
ValuesQuerySet JSON-response content
QuerySet JSON-response content
RelatedManager JSON-response content
Model Instance JSON-response content
file File-response content
generator JSON-response content
iterator JSON-response content
Any other object HTTP-response content

Examples

Basic

def view(request):
    return 401
def view(request):
    return 401, 'Not authorized'
def view(request):
    return {'foo': 'bar'}

Return model related resources

def view(request):
    return SomeModel.objects.all()
def view(request):
    return SomeModel.objects.get(pk=1)
def view(request):
    return SomeModel.objects.values()

Return a template

def view(request):
    return "template_name.html"
def view(request):
    return "template_name.html", {'foo': 'bar'}
def view(request):
    return 401, "template_name.html", {'foo': 'bar'}

The example above is the only case that view can return a tuple with 3 elements.