2017-12-19 1 views
0

나는 장고와 파이썬에 대해 아주 익숙하며, 메소드를 사용하여 문제를 적절히 정의했다. 내가 여기 어떻게 보이는지 또 다른 방법으로 그 방법을 방법으로 계산을 저장 .. 및 전화 싶습니다장고 앱의 다른 메소드에서 변수에 액세스하기

def get_info_array(self, format=None, *args, **kwargs): 
    current_response_list = get_current_team(self) 
    member_info_array = [] 
    for response_dic in current_response_list: 
     current_response = list(response_dic.values())[0] 
     chunk_score = get_chunk_score3() 
    print(chunk_score) 
    return chunk_score 


def get_chunk_score3(self, format=None, *args, **kwargs): 
    answer_question1 = current_response.answers.get(question_id = 2) 
    answer_question2 = current_response.answers.get(question_id = 3) 
    json_answer_question1 = json.loads(answer_question1.body) 
    json_answer_question2 = json.loads(answer_question2.body) 
    answer_key_question1 = list(json_answer_question1.keys())[0][0] 
    answer_key_question2 = list(json_answer_question2.keys())[0][0] 
    if answer_key_question1 == "1" or "3": 
     score1 = list(json_answer_question1.values())[0] 
    else: 
     score1 = -list(json_answer_question1.values())[0] 

     if answer_key_question2 == "1" or "3": 
      score2 = list(json_answer_question2.values())[0] 
     else: 
      score2 = -list(json_answer_question2.values())[0] 

      chunk_score = math.ceil((score1+score2)/2) 
     return chunk_score 

내가 current_response에 get_chunk_score3에서 정의되지 않는다는 것을 얻을 그 코드를 실행하려고, 다른 방법으로 변수에 액세스하려면 어떻게해야합니까?

아무 팁이나 진전을 환영합니다.

편집 : 전체 코드 :

class EmployeeChartData(APIView): 
    #import pdb; pdb.set_trace() 
    queryset = MyUser.objects.all() 
    serializer_class = MyUserSerializer 
    permission_classes = [] 
    http_method_names = ['get',] 

    #authentication_classes = [] 
    #permission_classes = [] 
    #serializer_class = MyUserSerializer 

    def get_serializer_class(self): 
     return self.serializer_class 



    def get(self, request, format=None, *args, **kwargs): 

     chunk2 = get_chunk_score2(self) 
     info2 = get_info_relationship2(self) 
     rep_system2 = get_rep_system2(self) 
     reality = get_reality_structure2(self) 
     scenario = get_scenario_thinking2(self) 
     percept = get_perceptual_category2(self) 

     data = { 

      "chunk2":chunk2 
     } 
     return Response(data) 

    def get_current_team(self, format=None, *args, **kwargs): 
    current_team_member = Project.objects.get(id = self.kwargs['pk1']).team_id.members.all() 
    members_response_list = [] 
    for member in current_team_member: 
     member_id = member.id 
     member_response = get_user_response(member_id) 
     members_response_list.append({member_id:member_response}) 

    return members_response_list 

def get_user_response(member_id): 
    current_user = MyUser.objects.get(id = member_id) #current_user 
    survey_team = Survey.objects.get(name= 'Survey SoftScore') #survey team (to change to final one) 
    current_response = ResponseModel.objects.filter(user = current_user, survey = survey_team)[0] 

return current_response 


def get_info_array(self, format=None, *args, **kwargs): 
       current_response_list = get_current_team(self) 
       member_info_array = [] 
       for response_dic in current_response_list: 
        current_response = list(response_dic.values())[0] 
        chunk_score = get_chunk_score3() 
        print(chunk_score) 
        return current_response_list 
def get_chunk_score3(): 
      answer_question1 = current_response.answers.get(question_id = 2) 
      answer_question2 = current_response.answers.get(question_id = 3) 
      json_answer_question1 = json.loads(answer_question1.body) 
      json_answer_question2 = json.loads(answer_question2.body) 
      answer_key_question1 = list(json_answer_question1.keys())[0][0] 
      answer_key_question2 = list(json_answer_question2.keys())[0][0] 
      if answer_key_question1 == "1" or "3": 
       score1 = list(json_answer_question1.values())[0] 
      else: 
       score1 = -list(json_answer_question1.values())[0] 

       if answer_key_question2 == "1" or "3": 
        score2 = list(json_answer_question2.values())[0] 
       else: 
        score2 = -list(json_answer_question2.values())[0] 

       chunk_score = math.ceil((score1+score2)/2) 

      return chunk_score  
+0

데이터베이스에 저장하고 다음보기의 데이터베이스에서 검색하십시오. – Stack

+0

다른 방법은 세션에 저장하는 것입니다 d up here https://stackoverflow.com/a/31707946/8150371 – Stack

+0

이 코드는 클래스 내에 있습니까? 동일한 .py 파일에 함수가 있습니까? –

답변

0

내가 전체 코드 가정은 EmployeeChartData 클래스의 일부이다.

current_response은 클래스의 어느 곳에도 정의되어 있지 않습니다. current_response을 반환하는 메서드 get_user_response()을 가지고 있기 때문에 get_user_response()을 호출하고 ResponseModel 개체를 current_response 변수에 get_chunk_score3()으로 할당하거나 get_user_response()으로 변경하여 개체를 클래스 변수에 할당해야합니다.

가장 간단한 해결책은 클래스 변수를 사용하는 것입니다. 당신은 current_response를 만든 다음 클래스를 통해 사용 :

class EmployeeChartData(APIView): 
    current_response = None # Default 

    def get_user_response(self, member_id): 
     current_user = MyUser.objects.get(id = member_id) #current_user 
     survey_team = Survey.objects.get(name= 'Survey SoftScore') #survey team (to change to final one) 
     self.current_response = ResponseModel.objects.filter(user = current_user, survey = survey_team)[0] # You assign ResponseModel object to current_response class variable 

    def get_chunk_score3(self): 
     answer_question1 = self.current_response.answers.get(question_id = 2) 

정말 클래스가 일할 수있는 표시되지 않습니다,하지만이 클래스 변수의 기본 생각이다. 나는 또한 당신이 class and instance variables에 대한 약간의 연구를 제안합니다.

0

은 당신이 당신은 여러 가지 방법으로 달성 할 수있는 변수 current_response 의 범위와 문제에 직면하고있다 생각 : 의이 두 가지 방법은 다음 @Borut에 의해 제안 된 방법을 따를 수 같은 클래스에있는 가정 해 봅시다.

그러나 코드 (세부 버전)에서 볼 수 있듯이 get_chunk_score3() 메서드는 staticmethod로 표시되지 않고 self을 포함하지 않습니다. 그래서 당신의 방법은 클래스의 일부가 아니라 같은 파일에 있다고 가정합니다. variables in python

주의 범위에

# imports 
current_response = None 
class myClass(object): 
    # ... class methods and/or variables 
    def get_info_array(self, format=None, *args, **kwargs): 
     # ...your operations 
     current_response = get_current_team(self) # this will assign value to global variable current_response 

def get_chunk_score3(): 
    # ... here you can access global value of current_response 

읽기 : 당신이 global variables을 사용할 수 있습니다 심지어 당신이 클래스를 사용하는 경우, 그러나 당신이 경우

그래서 당신은 다음과 같이 current_response라는 이름의 전역 변수를 정의 할 수 있습니다 클래스의 범위 밖에서 사용하지 않을 경우 클래스 내에 정의해야합니다.

관련 문제