python method() 正好接受 1 个参数(给定 2 个)

标签 python django

我在 django app views.py 中有一个这样的类

from dms.models import Folder, File, FileTag

class GetFamily:

    def getFathers(folder_id):
        if folder_id == None:
            rev_fathers=None
        else:
            fathers=[]
            rev_fathers=[]
            father=Folder.objects.get(id=folder_id)
            fathers.append(father)
            while father.parent_folder_id != None:
                father=Folder.objects.get(id=father.parent_folder_id)
                fathers.append(father)
            rev_fathers=reversed(fathers)
        return rev_fathers

    def getChildrenFolders(folder_id):
        folders=Folder.objects.filter(parent_folder_id=folder_id)
        return folders  

    def getChildrenFiles(folder_id):
        files=File.objects.filter(folder_id=folder_id)
        return files

当我使用 id 调用 getFathers(folder_id) 方法时,它给出 getFathers() 恰好接受 1 个参数(给定 2 个)

请帮帮我

最佳答案

getFathersGetFamily 类的方法,因此它需要实例的第一个参数。 python 中的约定是使用名称 self 作为此参数:

def getFathers(self, folder_id):

请注意,函数本身不访问任何类的成员。这通常是一个很好的指示,表明它不需要是类的方法,或者可以使用 @staticmethod 将其声明为静态方法。

关于python method() 正好接受 1 个参数(给定 2 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23826837/

相关文章:

python - 如何禁用 pyqt 中的特定标准按钮

python - 合并具有不同 left_on 和 right_on 长度列表的两个数据帧

java - 安装 python 插件后 Sonar 损坏 : The plugin python is not supported with Java 1. 6.0_29

python - 如何将 django 模型对象和相关对象转换为字典列表?

Django url反向: Non-reversible reg-exp portion: '(?='

python - pyCrypto 的可移植版本? - Python

python - 如何在 scipy 中创建评级 csr_matrix?

python - 如何动态定义 `typing.Union`?

Django 多个用户配置文件/子配置文件

python - 模板表单未在 Django 中呈现