Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django get_model()vs from.models导入somemodelname_Django_Django Models - Fatal编程技术网

Django get_model()vs from.models导入somemodelname

Django get_model()vs from.models导入somemodelname,django,django-models,Django,Django Models,使用get_model()的最佳实践是什么?何时导入 Ref:我更喜欢使用.models导入,因为这是获取模型对象的简单方法。 但是,如果您使用元类,可能,将是最好的选择 def get_model(self, app_label, model_name=None): """ Returns the model matching the given app_label and model_name. As a shortcut, this fun

使用get_model()的最佳实践是什么?何时导入


Ref:

我更喜欢使用.models导入,因为这是获取模型对象的简单方法。
但是,如果您使用元类,可能,将是最好的选择

def get_model(self, app_label, model_name=None):
        """
        Returns the model matching the given app_label and model_name.

        As a shortcut, this function also accepts a single argument in the
        form <app_label>.<model_name>.

        model_name is case-insensitive.

        Raises LookupError if no application exists with this label, or no
        model exists with this name in the application. Raises ValueError if
        called with a single argument that doesn't contain exactly one dot.
        """
        self.check_models_ready()
        if model_name is None:
            app_label, model_name = app_label.split('.')
        return self.get_app_config(app_label).get_model(model_name.lower())
def get_model(self,app_标签,model_name=None):
"""
返回与给定应用程序标签和模型名称匹配的模型。
作为快捷方式,此函数还接受

表单,也有帮助。

我更喜欢使用.models import,因为这是获取模型对象的简单方法。
但是,如果您使用元类,可能,将是最好的选择

def get_model(self, app_label, model_name=None):
        """
        Returns the model matching the given app_label and model_name.

        As a shortcut, this function also accepts a single argument in the
        form <app_label>.<model_name>.

        model_name is case-insensitive.

        Raises LookupError if no application exists with this label, or no
        model exists with this name in the application. Raises ValueError if
        called with a single argument that doesn't contain exactly one dot.
        """
        self.check_models_ready()
        if model_name is None:
            app_label, model_name = app_label.split('.')
        return self.get_app_config(app_label).get_model(model_name.lower())
def get_model(self,app_标签,model_name=None):
"""
返回与给定应用程序标签和模型名称匹配的模型。
作为快捷方式,此函数还接受

表单也可以提供帮助。

当需要动态获取模型类时,通常使用
get\u model()

一个实际示例:在为迁移编写
RunPython
操作时,您将应用程序注册表作为参数之一,并使用
apps.get_model('TheModel')
导入历史模型

另一个例子:你有一个应用程序,它动态构建了序列化程序,你将它们的
Meta.model
设置为你刚刚使用
get\u model()
得到的类

另一个例子是使用
self.get\u model()
导入
AppConfig.ready()
中的模型

需要记住的一点是,如果您使用的是
AppConfig.get_model()
apps.get_models()
,则只有在应用程序注册表完全填充后才能使用它们

另一个选项(
from.models import TheModel
)只是在代码中任何位置导入模型的默认方式


不过,这些只是示例,还有许多其他可能的场景。

当需要动态获取模型类时,通常使用
get\u model()

一个实际示例:在为迁移编写
RunPython
操作时,您将应用程序注册表作为参数之一,并使用
apps.get_model('TheModel')
导入历史模型

另一个例子:你有一个应用程序,它动态构建了序列化程序,你将它们的
Meta.model
设置为你刚刚使用
get\u model()
得到的类

另一个例子是使用
self.get\u model()
导入
AppConfig.ready()
中的模型

需要记住的一点是,如果您使用的是
AppConfig.get_model()
apps.get_models()
,则只有在应用程序注册表完全填充后才能使用它们

另一个选项(
from.models import TheModel
)只是在代码中任何位置导入模型的默认方式


不过,这些只是示例,还有许多其他可能的场景。

当需要动态获取模型类时,通常使用
get_model()
。例如,您有一个动态构建序列化程序的应用程序,您可以将其
Meta.model
设置为刚刚使用
get\u model()
获得的类。另一个选项是导入模型的默认方式。当需要动态获取模型类时,通常使用
get\u model()
。例如,您有一个动态构建序列化程序的应用程序,您可以将其
Meta.model
设置为刚刚使用
get\u model()
获得的类。另一个选项是导入模型的默认方式。这并没有回答他的问题,不是关于“更简单的方式”,而是关于良好实践以及何时使用它。我们有两个问题:使用get_model()的最佳实践是什么,何时导入?事实上,这不是一个好的做法,只有在使用时。如果你认为我的回答没有帮助,它完全有权投反对票。我很抱歉让你浪费时间阅读它。这并没有回答他的问题,不是关于“更简单的方法”,而是关于好的实践和使用它的时间。我们有两个问题:使用get_model()的最佳实践是什么,什么时候应该导入它?事实上,这不是一个好的做法,只有在使用时。如果你认为我的回答没有帮助,它完全有权投反对票。我很抱歉让你浪费时间阅读,这样就不能导入模型了?那么在这种情况下模型不能导入?