Read by linux/GNU commands

Let's follow and start from here:http://django-tastypie.readthedocs.org/en/latest/tutorial.html#creating-resources

According to tastypie's concept, Tastypie properly handles the Accept header.

So we can use linux/GNU commands to do some fancy things!

Bash script to get what we want.

And let's go and check it out.

it could be a fancy stuff.

Also we could see more from a bunch of other URLs available.

At this point, a bunch of other URLs are also available. Try out any/all of the following (assuming you have at least three records in the database):

Safe API

However, if you try sending a POST/PUT/DELETE to the resource, you find yourself getting “401 Unauthorized” errors. For safety, Tastypie ships with the authorization class (“what are you allowed to do”) set to ReadOnlyAuthorization. This makes it safe to expose on the web, but prevents us from doing POST/PUT/DELETE. Let’s enable those:

  1. # myapp/api.py
  2. from tastypie.authorization import Authorization
  3. from tastypie.resources import ModelResource
  4. from myapp.models import Entry
  5.  
  6. class EntryResource(ModelResource):
  7. class Meta:
  8. queryset = Entry.objects.all()
  9. resource_name = 'entry'
  10. authorization= Authorization()

Warning

This is now great for testing in development but VERY INSECURE. You should never put aResource like this out on the internet. Please spend some time looking at the authentication/authorization classes available in Tastypie.

Database to URL handling friendly

Creating More Resources

In order to handle our user relation, we’ll need to create a UserResource and tell the EntryResource to use it. So we’ll modify myapp/api.py to match the following code:

  1. # myapp/api.py
  2. from django.contrib.auth.models import User
  3. from tastypie import fields
  4. from tastypie.resources import ModelResource
  5. from myapp.models import Entry
  6.  
  7. class UserResource(ModelResource):
  8. class Meta:
  9. queryset = User.objects.all()
  10. resource_name = 'user'
  11.  
  12. class EntryResource(ModelResource):
  13. user = fields.ForeignKey(UserResource, 'user')
  14.  
  15. class Meta:
  16. queryset = Entry.objects.all()
  17. resource_name = 'entry'

We simply created a new ModelResource subclass called UserResource. Then we added a field toEntryResource that specified that the user field points to a UserResource for that data.

Now we should be able to get all of the fields back in our response. But since we have another full, working resource on our hands, we should hook that up to our API as well. And there’s a better way to do it.

Adding To The Api

Tastypie ships with an Api class, which lets you bind multiple Resources together to form a coherent API. Adding it to the mix is simple.

We’ll go back to our URLconf (urls.py) and change it to match the following:

  1. # urls.py
  2. from django.conf.urls.defaults import *
  3. from tastypie.api import Api
  4. from myapp.api import EntryResource, UserResource
  5.  
  6. v1_api = Api(api_name='v1')
  7. v1_api.register(UserResource())
  8. v1_api.register(EntryResource())
  9.  
  10. urlpatterns = patterns('',
  11. # The normal jazz here...
  12. (r'^blog/', include('myapp.urls')),
  13. (r'^api/', include(v1_api.urls)),
  14. )

Note that we’re now creating an Api instance, registering our EntryResource and UserResourceinstances with it and that we’ve modified the urls to now point to v1_api.urls.

This makes even more data accessible, so if we start up the runserver again, the following URLs should work:

Additionally, the representations out of EntryResource will now include the user field and point to an endpoint like /api/v1/users/1/ to access that user’s data. And full POST/PUT delete support should now work.

But there’s several new problems. One is that our new UserResource leaks too much data, including fields like emailpasswordis_active and is_staff. Another is that we may not want to allow end users to alter User data. Both of these problems are easily fixed as well.

URL pattern => Server Side => Database Collected => JSON Response

Let's now open this page,

http://127.0.0.1:8000/api/v1/user/1/?format=json

and it looks like this:

There is a good plugin in chrome which can make your JSON look better

https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc

Now let's go to the data base and compare what those are.

see the relationship in-between them.

in the database file   " db.sqlite3 " ,

let's see

all the user's data of user id=1 , are displayed in JSON format

database column name     represent       JSON   data  key            (IMPORTANT)

tastypie Django REST API developement 1)的更多相关文章

  1. 初识Django —Python API接口编程入门

    初识Django —Python API接口编程入门 一.WEB架构的简单介绍 Django是什么? Django是一个开放源代码的Web应用框架,由Python写成.我们的目标是用Python语言, ...

  2. tastypie Django REST framework API [Hello JSON]

    tastypie is a good thing. Haven't test it thoroughly. Gonna need some provement. Now I will introduc ...

  3. Django QuerySet API文档

    在查询时发生了什么(When QuerySets are evaluated) QuerySet 可以被构造,过滤,切片,做为参数传递,这些行为都不会对数据库进行操作.只要你查询的时候才真正的操作数据 ...

  4. tastypie Django REST framework

    Its one of the primary authors' lecture on pyCon: http://www.youtube.com/watch?v=Zv26xHYlc8s&nor ...

  5. $ Django 调API的几种方式

    API调用方式 下面是python中会用到的库.urllib2httplib2pycurlrequestsurllib2 #request import requests, json github_u ...

  6. day 68 django 之api操作 | jQueryset集合与对象

    我们的orm里面分为: jQueryset集合, 还有对象, 我们的jqueryset集合里面可以有多个对象,这句话的意思就是我们的对象是最小的单位,不可以再拆分了,我们的jQueryset集合就相当 ...

  7. python基于django编写api+前端后端分离

    有用 https://segmentfault.com/a/1190000016049962#articleHeader2 python的前后端分离(一):django+原生js实现get请求 htt ...

  8. Django SimpleCMDB API

    编写一个API,当我们访问 http://192.168.216.128:8000/hostinfo/getjson 时,返回 json 格式的主机组和组成员信息: [root@localhost S ...

  9. Django rest_framework API 随笔

    分页 需要对数量进行限制 ./settings.py REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination ...

随机推荐

  1. APlayer组件自制播放器

    .NET中使用APlayer组件自制播放器 2015-02-02 09:46 by xiaozhi_5638, 402 阅读, 9 评论, 收藏, 编辑 目录 说明 APlayer介绍 APlayer ...

  2. log4e插件的安装和使用

    1.首先下载log4e小工具.放入myeclipse10安装文件夹D:\Program Files (x86)\myEclipse10\MyEclipse Blue Edition 10\dropin ...

  3. C#播放流媒体的几种方法

    原文:[转载]C#播放流媒体的几种方法 做视频开发要学的东西真多,不知道如何入门,乱打乱撞,慢慢摸索吧! 首先搭建Windows Meida Server ,方法很简单,试试就会.在这里需要声明的是, ...

  4. DDD分层架构之仓储

    DDD分层架构之仓储(层超类型基础篇) 前一篇介绍了仓储的基本概念,并谈了我对仓储的一些认识,本文将实现仓储的基本功能. 仓储代表聚合在内存中的集合,所以仓储的接口需要模拟得像一个集合.仓储中有很多操 ...

  5. 6. SQL Server数据库监控 - 如何告警

    原文:6. SQL Server数据库监控 - 如何告警 常用的告警方式大致有:短信.邮件.应用程序 (beep提示,图标提示,升窗提示等),可是不能一直坐在电脑前看着应用程序,或者用脚本部署监控,根 ...

  6. Asterisk 未来之路3.0_0006

    原文:Asterisk 未来之路3.0_0006 Modules Asterisk 是基于模块构建的.一个模块提供某个特定的功能,它是动态的被装载.比如:信道驱动(chan_sip.so),或可以连接 ...

  7. android App Widgets

    http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#design http://develope ...

  8. 阻止check事件冒泡

    在Datagrid中添加了checkbox,想实现的效果是: 1.点击行中任意位置,该行的复选框变为选中状态,同时该行也获得焦点: 2.点击复选框,复选框打勾,同时该行获取焦点. 要实现功能1,用到d ...

  9. 在ubuntu下开发stm32f4-discovery

    前面零散地记录了一些如何安装编译器,调试器等笔记,这里就准备开始着手试一下这整块系统了. 简单不完全地回顾一下所需要安装的软件: 1 编译器 使用的是codesourcey,因为之前有使用过该套编译器 ...

  10. jquery背景动画插件使用

    在网页制作动画特效的时候,有时候想通过背景插入图片,然后通过控制背景显示的位置来实现一些动画效果,这样就不用使用绝对定位控制left和top来实现动画效果!但是jquery本身的动画函数是不支持背景动 ...