2012-10-25 78 views
2

我在我的Django應用程序中有以下模型。 BasicInfo和AddressInfo表使用Customer Table的id列(它們是這些表中的customer_id列)鏈接到Customer表。Django Tastypie:將多個模型添加到單個Tastypie資源

對於每個客戶,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。

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 

     } 
    ] 
} 

回答

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' 

當然,並添加根據需要對這些資源中的每一個進行認證/授權。

+0

我試過你的建議與資源。但當我打開以下網址: public-api/api/v0.1a/customer /?format = json 我收到消息 - 「模型」有一個空屬性'addressinfo_set',並且不允許一個空值。「 上面的消息是什麼意思?以及我如何解決這個問題的任何想法? –