mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 13:30:12 +03:00
Merge branch 'master' of https://github.com/tomchristie/django-rest-framework into model-tofield-serializer
This commit is contained in:
commit
77eeefab74
|
@ -60,7 +60,7 @@ For example, you can append `router.urls` to a list of existing views…
|
||||||
router.register(r'accounts', AccountViewSet)
|
router.register(r'accounts', AccountViewSet)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^forgot-password/$, ForgotPasswordFormView.as_view(),
|
url(r'^forgot-password/$', ForgotPasswordFormView.as_view(),
|
||||||
]
|
]
|
||||||
|
|
||||||
urlpatterns += router.urls
|
urlpatterns += router.urls
|
||||||
|
@ -68,14 +68,14 @@ For example, you can append `router.urls` to a list of existing views…
|
||||||
Alternatively you can use Django's `include` function, like so…
|
Alternatively you can use Django's `include` function, like so…
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^forgot-password/$, ForgotPasswordFormView.as_view(),
|
url(r'^forgot-password/$', ForgotPasswordFormView.as_view(),
|
||||||
url(r'^', include(router.urls))
|
url(r'^', include(router.urls))
|
||||||
]
|
]
|
||||||
|
|
||||||
Router URL patterns can also be namespaces.
|
Router URL patterns can also be namespaces.
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^forgot-password/$, ForgotPasswordFormView.as_view(),
|
url(r'^forgot-password/$', ForgotPasswordFormView.as_view(),
|
||||||
url(r'^api/', include(router.urls, namespace='api'))
|
url(r'^api/', include(router.urls, namespace='api'))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -198,7 +198,7 @@ Open the file `snippets/serializers.py` again, and replace the `SnippetSerialize
|
||||||
model = Snippet
|
model = Snippet
|
||||||
fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
|
fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
|
||||||
|
|
||||||
One nice property that serializers have is that you can inspect all the fields in a serializer instance, by printing it's representation. Open the Django shell with `python manage.py shell`, then try the following:
|
One nice property that serializers have is that you can inspect all the fields in a serializer instance, by printing its representation. Open the Django shell with `python manage.py shell`, then try the following:
|
||||||
|
|
||||||
>>> from snippets.serializers import SnippetSerializer
|
>>> from snippets.serializers import SnippetSerializer
|
||||||
>>> serializer = SnippetSerializer()
|
>>> serializer = SnippetSerializer()
|
||||||
|
|
|
@ -33,6 +33,14 @@ def unicode_to_repr(value):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def total_seconds(timedelta):
|
||||||
|
# TimeDelta.total_seconds() is only available in Python 2.7
|
||||||
|
if hasattr(timedelta, 'total_seconds'):
|
||||||
|
return timedelta.total_seconds()
|
||||||
|
else:
|
||||||
|
return (timedelta.days * 86400.0) + float(timedelta.seconds) + (timedelta.microseconds / 1000000.0)
|
||||||
|
|
||||||
|
|
||||||
# OrderedDict only available in Python 2.7.
|
# OrderedDict only available in Python 2.7.
|
||||||
# This will always be the case in Django 1.7 and above, as these versions
|
# This will always be the case in Django 1.7 and above, as these versions
|
||||||
# no longer support Python 2.6.
|
# no longer support Python 2.6.
|
||||||
|
|
|
@ -46,7 +46,7 @@ class BaseRenderer(object):
|
||||||
render_style = 'text'
|
render_style = 'text'
|
||||||
|
|
||||||
def render(self, data, accepted_media_type=None, renderer_context=None):
|
def render(self, data, accepted_media_type=None, renderer_context=None):
|
||||||
raise NotImplemented('Renderer class requires .render() to be implemented')
|
raise NotImplementedError('Renderer class requires .render() to be implemented')
|
||||||
|
|
||||||
|
|
||||||
class JSONRenderer(BaseRenderer):
|
class JSONRenderer(BaseRenderer):
|
||||||
|
|
|
@ -65,13 +65,13 @@ class BaseRouter(object):
|
||||||
If `base_name` is not specified, attempt to automatically determine
|
If `base_name` is not specified, attempt to automatically determine
|
||||||
it from the viewset.
|
it from the viewset.
|
||||||
"""
|
"""
|
||||||
raise NotImplemented('get_default_base_name must be overridden')
|
raise NotImplementedError('get_default_base_name must be overridden')
|
||||||
|
|
||||||
def get_urls(self):
|
def get_urls(self):
|
||||||
"""
|
"""
|
||||||
Return a list of URL patterns, given the registered viewsets.
|
Return a list of URL patterns, given the registered viewsets.
|
||||||
"""
|
"""
|
||||||
raise NotImplemented('get_urls must be overridden')
|
raise NotImplementedError('get_urls must be overridden')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def urls(self):
|
def urls(self):
|
||||||
|
|
|
@ -6,12 +6,13 @@ from django.db.models.query import QuerySet
|
||||||
from django.utils import six, timezone
|
from django.utils import six, timezone
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.utils.functional import Promise
|
from django.utils.functional import Promise
|
||||||
from rest_framework.compat import OrderedDict
|
from rest_framework.compat import OrderedDict, total_seconds
|
||||||
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
|
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
import types
|
import types
|
||||||
import json
|
import json
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
class JSONEncoder(json.JSONEncoder):
|
class JSONEncoder(json.JSONEncoder):
|
||||||
|
@ -41,10 +42,12 @@ class JSONEncoder(json.JSONEncoder):
|
||||||
representation = representation[:12]
|
representation = representation[:12]
|
||||||
return representation
|
return representation
|
||||||
elif isinstance(obj, datetime.timedelta):
|
elif isinstance(obj, datetime.timedelta):
|
||||||
return six.text_type(obj.total_seconds())
|
return six.text_type(total_seconds(obj))
|
||||||
elif isinstance(obj, decimal.Decimal):
|
elif isinstance(obj, decimal.Decimal):
|
||||||
# Serializers will coerce decimals to strings by default.
|
# Serializers will coerce decimals to strings by default.
|
||||||
return float(obj)
|
return float(obj)
|
||||||
|
elif isinstance(obj, uuid.UUID):
|
||||||
|
return six.text_type(obj)
|
||||||
elif isinstance(obj, QuerySet):
|
elif isinstance(obj, QuerySet):
|
||||||
return tuple(obj)
|
return tuple(obj)
|
||||||
elif hasattr(obj, 'tolist'):
|
elif hasattr(obj, 'tolist'):
|
||||||
|
|
|
@ -16,6 +16,9 @@ class ReturnDict(OrderedDict):
|
||||||
def copy(self):
|
def copy(self):
|
||||||
return ReturnDict(self, serializer=self.serializer)
|
return ReturnDict(self, serializer=self.serializer)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return dict.__repr__(self)
|
||||||
|
|
||||||
|
|
||||||
class ReturnList(list):
|
class ReturnList(list):
|
||||||
"""
|
"""
|
||||||
|
@ -27,6 +30,9 @@ class ReturnList(list):
|
||||||
self.serializer = kwargs.pop('serializer')
|
self.serializer = kwargs.pop('serializer')
|
||||||
super(ReturnList, self).__init__(*args, **kwargs)
|
super(ReturnList, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return list.__repr__(self)
|
||||||
|
|
||||||
|
|
||||||
class BoundField(object):
|
class BoundField(object):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user