简体   繁体   English

在单独的文件中拆分Django模型和模型形式

[英]Split Django models and modelforms in separate file

Is it possible to split Django models and modelforms in separate file without affecting performance. 是否可以在不影响性能的情况下将Django模型和模型形式拆分为单独的文件。

I've searched Django documentation, but can't find any clue. 我搜索了Django文档,但找不到任何线索。

Your question has more to do with how Python works than how to use Django. 您的问题更多地与Python如何工作有关,而不是与如何使用Django有关。 So before I explain how to apply imports in Django here is how you do them in Python. 因此,在我解释如何在Django中应用导入之前,这里是如何在Python中进行导入。

Python has extremely flexible modularization system - or a capability to break your code base into many files with hierarchy. Python具有极其灵活的模块化系统-或具有将您的代码库分解为具有层次结构的许多文件的功能。 This is rather necessary in any programming language because having your code in multiple files helps to organize things and it definitely helps when you have a group of people working on a project but the way Python does it is extremely easy and powerful (if you need it to be). 这在任何编程语言中都是非常必要的,因为将代码包含在多个文件中有助于组织事情,并且当您有一群人在从事项目工作时,这无疑会有所帮助,但是Python的方式极其简单且强大(如果需要)成为)。

The most basic component in Python import system are modules. Python导入系统中最基本的组件是模块。 Any Python file (with .py extension) is a Python module. 任何Python文件(扩展名为.py )都是Python模块。 The module name is just a name of the file excluding the extension. 模块名称只是不包括扩展名的文件的名称。 For example hello.py file is a hello Python module. 例如hello.py文件是一个hello Python模块。 Now modules/files usually have some variables/functions/classes defined inside of them. 现在,模块/文件通常在其中定义了一些变量/函数/类。 You can easily import those in other Python modules using an import statement. 您可以使用import语句轻松将其导入其他Python模块中。 Image you have two files in the same folder - foo.py and bar.py . 图像您在同一文件夹中有两个文件foo.pybar.py The following is a content of foo.py : 以下是foo.py的内容:

# foo.py

hello = 'world'

def sum(a, b):
    return a + b

Then if you need to use anything what is defined in foo.py in your bar.py file you can do so: 然后,如果您需要使用bar.py文件中foo.py中定义的任何内容,则可以这样做:

# bar.py

# the following import the whole foo module (foo.py file)
import foo

number = foo.sum(1, 2)

As you can see inside the bar.py , you can import the whole foo module and use anything what is defined in it. 如您在bar.py内部看到的bar.py ,您可以导入整个foo模块并使用其中定义的任何内容。 This however is usually not the best method to import things. 但是,这通常不是导入事物的最佳方法。 Imagine in your foo module you will have 10,000 variables defined however you only need to use one of them. 想象在您的foo模块中,您将定义10,000个变量,但是只需要使用其中一个即可。 So if you will import the whole module, Python will have to import all the variables which will obviously eat more memory then necessary. 因此,如果要导入整个模块,Python将必须导入所有变量,这显然会消耗更多的内存。 To solve that, you can selectively import objects from other objects using the from .. import .. statement. 为了解决这个问题,您可以使用from .. import ..语句有选择地从其他对象导入对象。 The following example in bar.py only import the sum function from module foo : bar.py的以下示例bar.py模块foo导入sum函数:

# bar.py

# import only the sum function from foo module
from foo import sum

number = foo.sum(1, 2)

So importing from Python modules is pretty easy. 因此,从Python模块导入非常简单。 Now to put that in your Django context, within your app you should have two files - models.py and forms.py . 我们把在你的Django情况下,你的应用程序中,你应该有两个文件- models.pyforms.py Inside models.py you should define all your models: models.py内部,您应该定义所有模型:

# models.py

from django.db import models

class FooModel(models.Model):
    pass

Within the models.py the way models is imported is different than what I showed before. models.py ,导入models的方式与我之前展示的不同。 In this case Python actually imports from a Python package however I will not get into that not to complicate things. 在这种情况下,Python实际上是从Python包中导入的,但是我不会为了不使事情复杂化而陷入困境。 Just take that import statement for granted. 只需将导入声明视为理所当然。

Now that you have models defined you can create forms for them in forms.py : 现在,您已经定义了模型,您可以在forms.py为它们创建表单:

# forms.py

# import django forms - take this statement for granted
from django import forms

# import the model
from models import FooModel

class FooModelForm(forms.ModelForm):
    class Meta:
        model = FooModel

That is it. 这就对了。 For more information about how Python does imports you can read the Dive Into Python book here (it also has a lot of other useful information...). 有关Python如何导入的更多信息,您可以在此处阅读Dive Into Python一书(它还有很多其他有用的信息...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM