2012-10-25 2 views
2

Django 애플리케이션에 다음 모델이 있습니다. BasicInfo 및 AddressInfo 테이블은 Customer 테이블의 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' 

모든 고객 테이블을 API로 제공 할 수있는 단일 Tastypie 리소스를 어떻게 만들 수 있습니까? 리소스 반환이 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 

     } 
    ] 
} 

답변

1

당신은 단순히 고객의 모든 나열하는 API를 요청하는 경우 :

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

줄 것입니다.

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' 

물론 추가 : api.py에서 고객의 엔드 포인트

를 볼 때 기본 정보/주소 정보를 표시하는 방법에 자원을 설정 - 그렇지 않으면, 아래 당신이 요구 한 가정 것입니다 필요에 따라 이러한 각 리소스에서 인증/권한 부여.

+0

나는 자원에 대한 귀하의 제안을 시도했습니다. 그러나 다음 URL을 열 때 : public-api/api/v0.1a/customer /? format = json "모델에 '빈 속성'addressinfo_set '이 있는데 허용하지 않습니다. null 값. " 위의 메시지는 무엇을 의미합니까? 어떻게 해결할 수 있을지 생각해? –