2

我的 Django 应用程序中有以下模型。BasicInfo 和 AddressInfo 表使用 Customer 表的 id 列链接到 Customer 表,该列是这些表中的 customer_id 列。

对于每个客户,AddressInfo 或 BasicInfo 表中可以有多个条目。

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 提供。

对于 id 为 12 的客户,资源返回的 json 对象可能类似于以下内容,

{
   "id": 12, 
   "joining_dtm": "2012-10-25T07:06:54.041528", 
   "resource_uri": "/public-api/api/v0.1a/customer/1/", 
   "AddressInfo": [
      {    
          "id":22,
          "apt_num": "54",
          "street": "Avondale Place",
          "street_2": "",
          "city": "Chicago",
          "country": "India",
          "postalcode": "600059",
          "customer_id": 12
       },
      {
          "id":96,
          "apt_num": "11",
          "street": "Infinite Loop",
          "street_2": "",
          "city": "Cupertino",
          "country": "USA",
          "postalcode": "123456",
          "customer_id": 12
       }
    ],

   "BasicInfo": [
      {
          "id": 33,
          "name": "My Full Name",
          "about": "Blah Blah Blah",
          "website": "http://google.com",
          "customer_id": 12
       },
      {
          "id": 147,
          "name": "My New Name",
          "about": "More Blah Blah",
          "website": "http://gmail.com",
          "customer_id": 12

       }
    ]
}
4

1 回答 1

1

如果您只是要求列出所有客户的 API:

/public-api/api/v0.1a/customer/?format=json

应该给你那个。否则,以下是我假设您要问的内容-查看客户端点时如何设置资源以显示基本信息/地址信息

在您的 api.py 中:

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'

当然,根据需要在这些资源中的每一个上添加身份验证/授权。

于 2012-10-25T07:35:54.847 回答