tastypie 多表关系

8 篇文章 0 订阅
5 篇文章 0 订阅
本文介绍了如何在Django工程中使用Tastypie解决多表关系通过外键查询相关联数据库的问题,包括安装Tastypie、配置urls.py、定义数据模型以及创建ModelResourcer的详细步骤。
摘要由CSDN通过智能技术生成

看了论坛上这篇文章,帮我解决了tastypie 多表关系通过外键查询相关联的数据库,

前置条件:

安装tastytie包:

sudo pip install django-tasypie

在django工程里面配置tastypie相关信息:

在主urls.py里面做如下配置把url和resource建立映射关系:

from tastypie.api import Api
from my_app.api.resources import MyModelResource

v1_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 ModelResource
from user_blog.models import Basicinfo,Addressinfo,Customer
from 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格式的信息



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值