Going live

This commit is contained in:
tom christie tom@tomchristie.com 2011-01-31 08:41:21 +00:00
parent 42f2f9b40d
commit 9024c2e94e
5 changed files with 43 additions and 4 deletions

View File

@ -1,6 +1,8 @@
ModelResource example - Blog posts
==================================
* http://api.django-rest-framework.org/blog-post/
The models
----------

View File

@ -1,6 +1,8 @@
Resource example - An object store
==================================
* http://api.django-rest-framework.org/object-store/
``urls.py``
.. include:: ../../examples/objectstore/urls.py

View File

@ -41,7 +41,26 @@ Getting Started
---------------
Often you'll want parts of your API to directly map to existing Models.
At it's simplest this looks something like this...
Typically that might look this looks something like this...
``models.py``
.. code-block:: python
from django.db import models
class MyModel(models.Model):
foo = models.BooleanField()
bar = models.IntegerField(help_text='Must be an integer.')
baz = models.CharField(max_length=32, help_text='Free text. Max length 32 chars.')
created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('created',)
@models.permalink
def get_absolute_url(self):
return ('simpleexample.views.MyModelResource', (self.pk,))
``urls.py``
@ -53,6 +72,22 @@ At it's simplest this looks something like this...
.. include:: ../examples/simpleexample/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.
We can visit the API in our browser:
* http://api.django-rest-framework.org/simple-example/
Or access it from the command line using curl:
.. code-block:: bash
bash: curl -X POST -H 'X-Requested-With: XMLHttpRequest' --data 'foo=bar' http://api.django-rest-framework.org/simple-example/
{"detail": {"bar": ["This field is required."], "baz": ["This field is required."]}}
.. note::
TODO: Mention adding custom handler methods, but that the defaults will often do what we want already. Document a Resource example, not tied to models.
Examples
--------

View File

@ -1,6 +1,6 @@
from django.db import models
MAX_INSTANCES = 20
MAX_INSTANCES = 10
class MyModel(models.Model):
foo = models.BooleanField()
@ -13,9 +13,9 @@ class MyModel(models.Model):
def save(self, *args, **kwargs):
"""For the purposes of the sandbox, limit the maximum number of stored models."""
super(MyModel, self).save(*args, **kwargs)
while MyModel.objects.all().count() > MAX_INSTANCES:
MyModel.objects.all()[0].delete()
super(MyModel, self).save(*args, **kwargs)
@models.permalink
def get_absolute_url(self):