mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 16:24:18 +03:00
Going live
This commit is contained in:
parent
42f2f9b40d
commit
9024c2e94e
|
@ -1,6 +1,8 @@
|
||||||
ModelResource example - Blog posts
|
ModelResource example - Blog posts
|
||||||
==================================
|
==================================
|
||||||
|
|
||||||
|
* http://api.django-rest-framework.org/blog-post/
|
||||||
|
|
||||||
The models
|
The models
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
Resource example - An object store
|
Resource example - An object store
|
||||||
==================================
|
==================================
|
||||||
|
|
||||||
|
* http://api.django-rest-framework.org/object-store/
|
||||||
|
|
||||||
``urls.py``
|
``urls.py``
|
||||||
|
|
||||||
.. include:: ../../examples/objectstore/urls.py
|
.. include:: ../../examples/objectstore/urls.py
|
||||||
|
|
|
@ -41,7 +41,26 @@ Getting Started
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
Often you'll want parts of your API to directly map to existing Models.
|
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``
|
``urls.py``
|
||||||
|
|
||||||
|
@ -53,6 +72,22 @@ At it's simplest this looks something like this...
|
||||||
.. include:: ../examples/simpleexample/views.py
|
.. include:: ../examples/simpleexample/views.py
|
||||||
:literal:
|
: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
|
Examples
|
||||||
--------
|
--------
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
MAX_INSTANCES = 20
|
MAX_INSTANCES = 10
|
||||||
|
|
||||||
class MyModel(models.Model):
|
class MyModel(models.Model):
|
||||||
foo = models.BooleanField()
|
foo = models.BooleanField()
|
||||||
|
@ -13,9 +13,9 @@ class MyModel(models.Model):
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
"""For the purposes of the sandbox, limit the maximum number of stored models."""
|
"""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:
|
while MyModel.objects.all().count() > MAX_INSTANCES:
|
||||||
MyModel.objects.all()[0].delete()
|
MyModel.objects.all()[0].delete()
|
||||||
super(MyModel, self).save(*args, **kwargs)
|
|
||||||
|
|
||||||
@models.permalink
|
@models.permalink
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user