【问题标题】:how call a function with in the class in djagno shell如何在 django shell 的类中调用函数
【发布时间】:2021-08-05 08:04:44
【问题描述】:
class Test:
    def m1():
        pass
    def m2():
        pass

我在 test.py(Django-Project) 文件中有这个类如何在 Django 中调用 m2 函数。贝壳。如何在 Django shell 中调用这个函数。

我正在尝试这样:

>> python manage.py shell
>> from appname.filename import <class_name|function_name>

但是它抛出了这样的错误:ImportError: cannot import name 。任何人都可以帮我解决这个问题。提前致谢。

【问题讨论】:

    标签: python python-3.x django django-rest-framework django-views


    【解决方案1】:

    您需要从模块中导入类,而不是类中的函数。这样的事情可能会奏效。

    # myfile.py
    
    class TestClass:
        def func1():
            pass
    
    from myfile import TestClass
    
    test_class = TestClass()
    test_class.func1()
    

    【讨论】:

    • 非常感谢@crintus,它对我有用。
    猜你喜欢
    • 2022-01-21
    • 2012-11-09
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多