2014-11-07 4 views
1

내 tastypie 코드 스 니펫은 다음과 같습니다.Django Tastypie의 Resource 인스턴스를 호출하는 방법.?

나는 리소스가 있고 post_list 메서드에는 Mysample 인스턴스가 생성됩니다.

내가 Mysample 인스턴스의 메소드를 호출 할, 내가 Mysample 예를

class MysampleResource(ModelResource): 
    intfeild1 = fields.IntegerField('intfeild1_id', null=True) 
    intfeild2 = fields.IntegerField('intfeild1_id') 

    class Meta: 
     always_return_data = True 
     queryset = Mysample.objects.all() 
     allowed_methods = ['get','post','put','delete',] 
     authentication = SessionAuthentication() 
     authorization = MysampleAuthorization() 


    def post_list(self, request, **kwargs): 

      result = super(MysampleResource, self).post_list(request, **kwargs) 

      #here I want to call a method of Mysample Instance. 
      return result 
의 방법을하시기 바랍니다 호출 할 필요가 어디

코드에서 주석을 찾아주십시오, 어떻게 그렇게 제발 도와주세요 도와주세요, 저는 초보자입니다. 따라서 어떤 방법을 대체 할 것인지, 어디에서해야하는지 제안 해주십시오. 당신이 그것을 호출 할 수 있습니다
def test_method(self,param*): 
     #Do your stuff 
     return result 

및 post_list 내

좋아 :

+0

당신은 어떤 오류가 있습니까이? any.It가 나오면 오류를 추가하십시오. 언급 한대로 작동해야합니다. –

답변

1

당신은 당신의 자원에 메소드를 추가 할 필요가

self.test_method(param*) 

참고 :이 방법 선언은하지만 파이썬에서 2 PARAMS 포함 " self "는 암시 적 매개 변수로 전달되므로 메서드 호출시 자체 객체를 전달하지 않아야합니다.

  • =이 경우 하나의 매개 변수 이상일 수 있습니다. ","를 사용하여 매개 변수를 구분하십시오.

우리는 당신의 코드가 같아야 이전의 모든 개념을 적용하는 경우 :

class MysampleResource(ModelResource): 
    intfeild1 = fields.IntegerField('intfeild1_id', null=True) 
    intfeild2 = fields.IntegerField('intfeild1_id') 

    class Meta: 
     always_return_data = True 
     queryset = Mysample.objects.all() 
     allowed_methods = ['get','post','put','delete',] 
     authentication = SessionAuthentication() 
     authorization = MysampleAuthorization() 


     def post_list(self, request, **kwargs): 

       result = super(MysampleResource, self).post_list(request, **kwargs) 

       #Let's say that you want to pass resquest as your param to your method 
       method_result=self.test_method(request) 
       return result 

     def test_method(self,request): 
       #Do your stuff 
       return result