Django-Tastypie Introduction and Use Guide
Django-Tastypie is a Django extension framework for building Restful API.It provides developers with a simple and flexible way to create and manage the Web API, and seamlessly integrates the ORM (object relationship mapping) system with Django.
Using Django-Tastypie, you can define the data structure of the API by using a predetermined resource model.These resource models allow you to specify the operation of CRUD (creation, reading, update, deletion) operation of resources, and provide a set of tools for verification, authorization and filtering query.In addition, it also supports query parameters for search and sorting.
In order to start using Django-Tastypie, you need to follow the steps below:
The first step is to install Django-Tastypie.You can install it by running the following commands on the terminal:
shell
pip install django-tastypie
The second step is to add the `tastypie` to the` Installed_apps` configuration of your Django project.Open the project's `settings.py` file, find the` Installed_apps, and add the `tastypie` to it, as shown below:
python
INSTALLED_APPS = [
# ...
'tastypie',
]
The third step is to define the API resource model.In Django-Tastypie, each resource corresponds to a Django model and is created by inheriting the `Tastypie.Resources.modelresource` class.The following is the code of a sample resource model:
python
from tastypie.resources import ModelResource
from myapp.models import MyModel
class MyModelResource(ModelResource):
class Meta:
queryset = MyModel.objects.all()
resource_name = 'mymodel'
In the above code, we define a resource model called `mymodelresource`, which corresponds to the` mymodel` model.We designated the query set `querySet` to` mymodel.objects.all () `, which will return the object of all` mymodel` models.`resource_name` attribute specifies the resource name used in the API.
The fourth step is to define the API URL.In the `urls.py` file of the Django project, you need to add the URL configuration of Django-Tastypie to it.The following is a code configuration:
python
from django.conf.urls import url, include
from tastypie.api import Api
from myapp.api import MyModelResource
v1_api = Api(api_name='v1')
v1_api.register(MyModelResource())
urlpatterns = [
# ...
url(r'^api/', include(v1_api.urls)),
]
In the above code, we first introduced the `tastypie.api.api` class and the resource model class we defined.Then, we instance a `API" object, and register the resource model into the API by calling the `register ()` method.Finally, we use the `Include ()` function to configure the API URL configuration to the URL mode of the project.
Through the above settings, your Django-Tastypie API is ready.You can access the `http:// localhost: 8000/API/V1/MyModel/` (assuming the Django development server is running on the 8000 port on the local host) to view the list of resource lists of the API.You can also use other HTTP methods, such as get, post, put, and delete to operate resources.
This is just the basic usage of Django-Tastypie. It also has many other functions and configuration options, such as custom verification, authorization, filtering, searching, and sorting.You can refer to the official documentation of Django-Tastypie to obtain more detailed information and example code.