mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 13:14:30 +03:00
Updating docs for 0.2
This commit is contained in:
parent
f78076b5ba
commit
bf9ea978bc
|
@ -44,7 +44,7 @@ def _model_to_dict(instance, resource=None):
|
||||||
include = resource and resource.include or ()
|
include = resource and resource.include or ()
|
||||||
exclude = resource and resource.exclude or ()
|
exclude = resource and resource.exclude or ()
|
||||||
|
|
||||||
extra_fields = fields and list(resource.fields) or []
|
extra_fields = fields and list(fields) or list(include)
|
||||||
|
|
||||||
# Model fields
|
# Model fields
|
||||||
for f in opts.fields + opts.many_to_many:
|
for f in opts.fields + opts.many_to_many:
|
||||||
|
@ -62,6 +62,7 @@ def _model_to_dict(instance, resource=None):
|
||||||
|
|
||||||
# Method fields
|
# Method fields
|
||||||
for fname in extra_fields:
|
for fname in extra_fields:
|
||||||
|
try:
|
||||||
if hasattr(resource, fname):
|
if hasattr(resource, fname):
|
||||||
# check the resource first, to allow it to override fields
|
# check the resource first, to allow it to override fields
|
||||||
obj = getattr(resource, fname)
|
obj = getattr(resource, fname)
|
||||||
|
@ -78,6 +79,10 @@ def _model_to_dict(instance, resource=None):
|
||||||
# Let's keep _model_to_dict flat, and _object_to_data recursive.
|
# Let's keep _model_to_dict flat, and _object_to_data recursive.
|
||||||
data[fname] = _object_to_data(obj)
|
data[fname] = _object_to_data(obj)
|
||||||
|
|
||||||
|
except NoReverseMatch:
|
||||||
|
# Ug, bit of a hack for now
|
||||||
|
pass
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
@ -223,7 +228,7 @@ class FormResource(Resource):
|
||||||
|
|
||||||
# In addition to regular validation we also ensure no additional fields are being passed in...
|
# In addition to regular validation we also ensure no additional fields are being passed in...
|
||||||
unknown_fields = seen_fields_set - (form_fields_set | allowed_extra_fields_set)
|
unknown_fields = seen_fields_set - (form_fields_set | allowed_extra_fields_set)
|
||||||
unknown_fields = unknown_fields - set(('csrfmiddlewaretoken', '_accept')) # TODO: Ugh.
|
unknown_fields = unknown_fields - set(('csrfmiddlewaretoken', '_accept', '_method')) # TODO: Ugh.
|
||||||
|
|
||||||
# Check using both regular validation, and our stricter no additional fields rule
|
# Check using both regular validation, and our stricter no additional fields rule
|
||||||
if bound_form.is_valid() and not unknown_fields:
|
if bound_form.is_valid() and not unknown_fields:
|
||||||
|
@ -437,6 +442,9 @@ class ModelResource(FormResource):
|
||||||
This method can be overridden if you need to set the resource url reversing explicitly.
|
This method can be overridden if you need to set the resource url reversing explicitly.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if not hasattr(self, 'view_callable'):
|
||||||
|
raise NoReverseMatch
|
||||||
|
|
||||||
# dis does teh magicks...
|
# dis does teh magicks...
|
||||||
urlconf = get_urlconf()
|
urlconf = get_urlconf()
|
||||||
resolver = get_resolver(urlconf)
|
resolver = get_resolver(urlconf)
|
||||||
|
|
|
@ -32,13 +32,6 @@ Here's the models we're working from in this example. It's usually a good idea
|
||||||
.. include:: ../../examples/modelresourceexample/models.py
|
.. include:: ../../examples/modelresourceexample/models.py
|
||||||
:literal:
|
:literal:
|
||||||
|
|
||||||
Now that we've got some models and a urlconf, there's very little code to write. We'll create a :class:`.ModelResource` to map to instances of our models, and a top level :class:`.RootModelResource` to list the existing instances and to create new instances.
|
|
||||||
|
|
||||||
``views.py``
|
|
||||||
|
|
||||||
.. include:: ../../examples/modelresourceexample/views.py
|
|
||||||
:literal:
|
|
||||||
|
|
||||||
And we're done. We've now got a fully browseable API, which supports multiple input and output media types, and has all the nice automatic field validation that Django gives us for free.
|
And we're done. We've now got a fully browseable API, which supports multiple input and output media types, and has all the nice automatic field validation that Django gives us for free.
|
||||||
|
|
||||||
We can visit the API in our browser:
|
We can visit the API in our browser:
|
||||||
|
|
|
@ -70,29 +70,23 @@ For more information on settings take a look at the :ref:`setup` section.
|
||||||
Getting Started
|
Getting Started
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
Using Django REST framework can be as simple as adding a few lines to your urlconf and adding a `permalink <http://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url>`_ to your model.
|
Using Django REST framework can be as simple as adding a few lines to your urlconf.
|
||||||
|
|
||||||
`urls.py`::
|
``urls.py``::
|
||||||
|
|
||||||
from django.conf.urls.defaults import patterns, url
|
from django.conf.urls.defaults import patterns, url
|
||||||
from djangorestframework import ModelResource, RootModelResource
|
from djangorestframework.resources import ModelResource
|
||||||
from models import MyModel
|
from djangorestframework.views import ListOrCreateModelView, InstanceModelView
|
||||||
|
from myapp.models import MyModel
|
||||||
|
|
||||||
|
class MyResource(ModelResource):
|
||||||
|
model = MyModel
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^$', RootModelResource.as_view(model=MyModel)),
|
url(r'^$', RootModelResource.as_view(resource=MyResource)),
|
||||||
url(r'^(?P<pk>[^/]+)/$', ModelResource.as_view(model=MyModel), name='my-model'),
|
url(r'^(?P<pk>[^/]+)/$', ModelResource.as_view(resource=MyResource)),
|
||||||
)
|
)
|
||||||
|
|
||||||
`models.py`::
|
|
||||||
|
|
||||||
class MyModel(models.Model):
|
|
||||||
|
|
||||||
# (Rest of model definition...)
|
|
||||||
|
|
||||||
@models.permalink
|
|
||||||
def get_absolute_url(self):
|
|
||||||
return ('my-model', (self.pk,))
|
|
||||||
|
|
||||||
Django REST framework comes with two "getting started" examples.
|
Django REST framework comes with two "getting started" examples.
|
||||||
|
|
||||||
#. :ref:`resources`
|
#. :ref:`resources`
|
||||||
|
|
Loading…
Reference in New Issue
Block a user