2

私のDjangoアプリケーションには次のモデルがあります。BasicInfo テーブルと AddressInfo テーブルは、Customer テーブルの id 列 (これらのテーブルの customer_id 列) を使用して Customer テーブルにリンクされています。

顧客ごとに、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'

すべての顧客テーブルを API として提供できる単一の Tastypie リソースを作成するにはどうすればよいですか。

リソースが返す json オブジェクトは、ID が 12 の顧客の場合、次のようなものになります。

{
   "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 に答える