tastypie 多表关系
来源:互联网 发布:淘宝会员怎么取消 编辑:程序博客网 时间:2024/06/06 17:50
看了论坛上这篇文章,帮我解决了tastypie 多表关系通过外键查询相关联的数据库,
前置条件:
安装tastytie包:
sudo pip install django-tasypie
在django工程里面配置tastypie相关信息:
在主urls.py里面做如下配置把url和resource建立映射关系:
from tastypie.api import Apifrom my_app.api.resources import MyModelResourcev1_api = Api(api_name='v1')v1_api.register(CustomerResource())v1_api.register(AddressInfoResource())v1_api.register(BasicInfoResource())urlpatterns = patterns('', # ...more URLconf bits here... # Then add: (r'^api/', include(v1_api.urls)),)
三个数据模型如下:
class Customer(models.Model): id = models.IntegerField(primary_key=True) joining_dtm = models.DateTimeField() isactive = models.IntegerField() class Meta: db_table = u'Customer'class Addressinfo(models.Model): id = models.IntegerField(primary_key=True) apt_num = models.CharField(max_length=135, blank=True) street = models.CharField(max_length=135, blank=True) street_2 = models.CharField(max_length=135, blank=True) city = models.CharField(max_length=135, blank=True) country = models.CharField(max_length=135, blank=True) postalcode = models.CharField(max_length=135, blank=True) customer_id = models.ForeignKey(Customer) class Meta: db_table = u'AddressInfo'class Basicinfo(models.Model): id = models.IntegerField(primary_key=True) name = models.CharField(max_length=135, blank=True) about = models.CharField(max_length=135, blank=True) website = models.CharField(max_length=135, blank=True) customer_id = models.ForeignKey(Customer) class Meta: db_table = u'BasicInfo'
tastypie api.py的ModelResourcer内容如下:
from tastypie.resources import ModelResourcefrom user_blog.models import Basicinfo,Addressinfo,Customerfrom tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
class CustomerResource(ModelResource): # The 2nd parameter is tricky: # Try suffixing with nothing, 's', or '_set' # e.g. 'addressinfo', 'addressinfos', 'addressinfo_set' addressinfos = fields.ToManyField('your_app_name.api.AddressInfoResource', 'addressinfo_set', Full=True) basicinfos = fields.ToManyField('your_app_name.api.BasicInfoResource', 'basicinfo_set', Full=True) class Meta: queryset = Customer.objects.all() resource_name = 'customer'class AddressInfoResource(ModelResource): class Meta: queryset = Addressinfo.objects.all() resource_name = 'addressinfo'class BasicInfoResource(ModelResource): class Meta: queryset = Basicinfo.objects.all() resource_name = 'basicinfo'
127.0.0.1:8000/api/v0.1a/customer/?format=json
就可以得到与Customer外键相关联AddressInfo和BasicInfo表的json格式的信息 0 0
- tastypie 多表关系
- Django RESTful 使用tastypie
- 一个简单的Tastypie
- Django Tastypie 知识总结
- Tastypie的Django View方法
- Django Tastypie 构建Django RESTful API
- 对于django的tastypie的理解
- Tastypie 资源过滤器的相关机制介绍
- Django RESTfull接口之Tastypie简介(一)
- Django 1.5.4 专题13 Web Service with tastypie
- django tastypie backbone.js todos 例子调试成功
- django tastypie filtering 数据过滤(条件查询)
- How to filter objects by user id with tastypie?
- Hibernate多表映射关系
- SQl数据库多表关系
- mybatis一对多关系表
- 表关系
- 表关系
- 提高增量备份的速度
- java多线程之利用“CyclicBarrier”汇总结果
- wxPython中的布局
- Java集合关系图
- Python-OpenCV教程-2
- tastypie 多表关系
- 【codevs】2597 团伙
- linux内核配置与编译即根文件制作
- Error: opening registry key 'Software\JavaSoft\Java Runtime Environment'
- 我的 Sublime text 之路
- POJ1094(模拟····拓扑)
- express学习笔记
- Android Activities
- Windows 10预览版系统ISO正式开放下载