从django app

时间:2017-12-19 17:42:26

标签: python django

我是django和python的新手,我有一个问题,使用方法并正确定义它们。 我想在一个方法中存储计算..并在这里调用该方法的另一种方法是它的外观:

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

当我尝试运行该代码时,我得到get_chunk_score3中的current_response中没有定义,如何从其他方法访问变量?

欢迎任何提示进展。

编辑: 完整代码:

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    

2 个答案:

答案 0 :(得分:0)

我假设整个代码都是EmployeeChartData类的一部分。

current_response未在班级的任何位置定义。您有一个返回get_user_response()的方法current_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进行一些研究。

答案 1 :(得分:0)

我认为你面临变量current_response范围的问题 您可以通过不同方式实现此目的: 我们假设两种方法属于同一类,那么您可以按照@Borut建议的方式进行操作。

但是,如您的代码(详细版本)所示,您的方法get_chunk_score3()未标记为staticmethod且未包含self;所以我假设你的方法不是类的一部分,而是在同一个文件中。

因此,您可以按如下方式定义名为current_response的全局变量:

# 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

阅读variables in python

的范围

注意:即使您正在使用类,也可以使用global variables,但如果您不打算在类的范围之外使用它,那么您应该只在类中定义它