2016-01-30 46 views
2

我有兩個資源,客戶和電話(我只是通過只包括幾個字段來簡化它)。不同的客戶可以擁有相同類型的電話。我寫了我的Modelresource類,並通過/ customer /和/ phone訪問API。我現在想做的是爲某個客戶獲取電話。 so/customer/1/phone/嵌套資源用於美食中的多對多關係。父母子女關係

這些是我的類的樣子。

Models.py

# Defines the phone Model 

class Phone(models.Model): 
    phone_id= models.AutoField(primary_key=True) 
    phone_type = models.CharField(max_length=100) 


# Defines the Customer Model 

class Customer(models.Model): 
    customer_id= models.AutoField(primary_key=True) 
    phone = models.ManyToManyField(Phone) 

Api.py

class PhoneResource(ModelResource): 
    class Meta: 
     queryset = Phone.objects.all() 
     allowed_methods = ['get'] 
     resource_name = 'phone' 

class CustomerResource(ModelResource): 
    phone = fields.ManyToManyField(PhoneResource, "phone") 

    class Meta: 
     queryset = Customer.objects.all() 
     allowed_methods = ['get', 'patch', 'put'] 
     resource_name = 'customer' 
     authentication = Authentication() 
     authorization = Authorization() 

    def prepend_urls(self): 
     return [ 
      url(r'^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/phone%s$' % 
       (self._meta.resource_name, trailing_slash()), 
       self.wrap_view('get_customer_phone'), name='customer_phone'), 
     ] 

    def customer_phone(self, request, **kwargs): 
     # My Question is what goes in this function 
     # I want to get only the phones for the given customer, and exclude other phones that does not belong to them 

我已經調查http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources

但它不工作。我一直在收回所有電話,而不僅僅是屬於某個客戶的電話。所以,如果約翰有一個android和一個ios,它應該返回這兩個列表,但如果約翰有android它應該只返回android。但是,我正在使用手機模型中的所有手機。

+0

這是什麼'customer_phone()'目前看起來像? –

+0

讓它工作的唯一方法是從客戶資源創建一個捆綁包,然後刪除除電話字段之外的所有字段,然後返回該捆綁。這個問題是,我想/客戶返回電話資源作爲uri,和/客戶/ 1 /電話/返回完整。但是我必須要麼兩者都是完整的,要麼兩者都是uris。 – Purpamine

+0

必須有比這更好的解決方案... – Purpamine

回答

1

型號:

class Phone(models.Model): 
    phone_id= models.AutoField(primary_key=True) 
    phone_type = models.CharField(max_length=100) 


# Defines the Customer Model 

class Customer(models.Model): 
    customer_id= models.AutoField(primary_key=True) 
    phones = models.ManyToManyField(Phone, related_name='customers') 

API:

class PhoneResource(ModelResource): 
    # TODO: update path 
    customers = fields.ManyToManyField('path.to.CustomerResource', "customers") 

    class Meta: 
     queryset = Phone.objects.all() 
     allowed_methods = ['get'] 
     resource_name = 'phone' 

class CustomerResource(ModelResource): 
    phones = fields.ManyToManyField(PhoneResource, "phones") 

    class Meta: 
     queryset = Customer.objects.all() 
     allowed_methods = ['get', 'patch', 'put'] 
     resource_name = 'customer' 
     authentication = Authentication() 
     authorization = Authorization() 

    def prepend_urls(self): 
     return [ 
      url(r'^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/phone%s$' % 
       (self._meta.resource_name, trailing_slash()), 
       self.wrap_view('get_customer_phone'), name='customer_phone'), 
     ] 

    def get_customer_phone(self, request, **kwargs): 
     try: 
      bundle = self.build_bundle(data={'pk': kwargs['pk']}, request=request) 
      obj = self.cached_obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs)) 
     except ObjectDoesNotExist: 
      return HttpGone() 
     except MultipleObjectsReturned: 
      return HttpMultipleChoices("More than one resource is found at this URI.") 

     phone_resource = PhoneResource() 
     return phone_resource.get_list(request, customers=obj.pk) 
+0

我給了這個鏡頭,我不知道這可以工作。正如您可以看到客戶模型有電話,但手機型號沒有客戶。那我怎麼能得到這個customer_set關係。這是我無法弄清的最重要的事情!我唯一能做的就是在客戶資源中的phone = fields.ManyToManyField(PhoneResource,「phone」,related_name =「phone」)。這甚至是正確的嗎? – Purpamine

+0

Django自動添加反向關係: https://docs.djangoproject.com/en/1.9/ref/models/relations/ https://docs.djangoproject.com/es/1.9/topics/db/queries/ #lookups-that-span-relationships –

+0

沒問題。我仍然無法爲客戶取回所有電話。保持返回模型中的所有手機... – Purpamine

0

您確定需要單獨的prepend_urls嗎?你可以得到的電話清單爲每個客戶購買添加全=真到ManyToManyField參數:

class CustomerResource(ModelResource): 
    phone = fields.ManyToManyField(PhoneResource, "phone", full=True) 

    class Meta: 
     queryset = Customer.objects.all() 
     allowed_methods = ['get', 'patch', 'put'] 
     resource_name = 'customer' 
     authentication = Authentication() 
     authorization = Authorization() 
+0

但是,這也將返回其他領域。我只想拿回手機。所以如果客戶有名字和姓氏,那麼當我只需要他們的電話時,做一個返回所有字段的呼叫是沒有意義的。這就是爲什麼我想要做一個嵌套的網址/客戶/ 1 /電話/ – Purpamine