I'm just working my way through Django, and really liking it so far, but I have an issue and I'm not sure what the typical way to solve it.
我只是在《被解救的姜戈》中工作,到目前为止我真的很喜欢它,但是我有一个问题,我不确定解决它的典型方法是什么。
Suppose I have a View which is supposed to be updated when some complex Python object is updated, but this object is not driven by the database, say it is driven by AJAX calls or directly by the user or something.
假设我有一个视图,它应该在更新复杂的Python对象时更新,但是这个对象不是由数据库驱动的,比如它是由AJAX调用驱动的,或者直接由用户或其他什么东西驱动的。
Where does this code go? Should it still go in models.py????
这段代码到哪里去了?它还应该用model .py吗?
1 个解决方案
#1
36
Your models.py
can be (and sometimes is) empty. You are not obligated to have a model which maps to a database.
您的模型。py可以是(有时是)空的。您不必拥有映射到数据库的模型。
You should still have a models.py
file, to make Django's admin happy. The models.py
file name is important, and it's easier to have an empty file than to try and change the file expected by various admin commands.
你还应该有一个模型。py文件,让Django的管理员高兴。的模型。py文件名称很重要,它更容易拥有一个空文件,而不是尝试更改各种管理命令所期望的文件。
The "model" -- in general -- does not have to map to a database. The "model" -- as a general component of MVC design -- can be anything.
一般来说,“模型”不需要映射到数据库。“模型”——作为MVC设计的一个通用组件——可以是任何东西。
You can -- and often do -- define your own "model" module that your views use. Just don't call it models.py
because it will confuse Django admin. Call it something meaningful to your application: foo.py
. This foo.py
manipulates the real things that underpin your application -- not necessarily a Django Model.model
subclass.
您可以——通常是这样——定义您的视图使用的自己的“模型”模块。不要叫它模型。因为它会混淆Django admin。可以将其称为对应用程序有意义的东西:foo.py。这foo。py操纵支撑应用程序的真实事物——不一定是Django模型。模型子类。
Django MVC does not require a database mapping. It does explicitly expect that the module named models.py
has a database mapping in it. So, use an empty models.py
if you have no actual database mapping.
Django MVC不需要数据库映射。它明确地期望模块命名为models。py中有一个数据库映射。所以,使用一个空模型。如果没有实际的数据库映射,则为py。
Your views.py
can use
你的观点。py可以使用
import foo
def index( request ):
objects = foo.somelistofobjects()
*etc.*
Django allows you to easily work with no database mapping. Your model can easily be anything. Just don't call it models.py
.
Django允许在没有数据库映射的情况下轻松工作。你的模型可以是任何东西。不要叫它model .py。
Edit.
编辑。
Are Views registered with Models? No.
视图是否与模型注册?不。
On update to the Model by the Controller the Views get notified? No.
当控制器更新模型时,视图会被通知吗?不。
Is the Model strictly the data respresentation as this is really MVP? Yes.
模型是否严格的数据响应,因为这是真正的MVP?是的。
Read the Django docs. It's simple.
Django文档阅读。这很简单。
Web Request -> URL mapping -> View function -> Template -> Response.
Web请求-> URL映射->视图功能->模板->响应。
The model can be used by the view function. The model can be a database mapping, or it can be any other thing.
该模型可由视图函数使用。模型可以是数据库映射,也可以是其他任何东西。
#1
36
Your models.py
can be (and sometimes is) empty. You are not obligated to have a model which maps to a database.
您的模型。py可以是(有时是)空的。您不必拥有映射到数据库的模型。
You should still have a models.py
file, to make Django's admin happy. The models.py
file name is important, and it's easier to have an empty file than to try and change the file expected by various admin commands.
你还应该有一个模型。py文件,让Django的管理员高兴。的模型。py文件名称很重要,它更容易拥有一个空文件,而不是尝试更改各种管理命令所期望的文件。
The "model" -- in general -- does not have to map to a database. The "model" -- as a general component of MVC design -- can be anything.
一般来说,“模型”不需要映射到数据库。“模型”——作为MVC设计的一个通用组件——可以是任何东西。
You can -- and often do -- define your own "model" module that your views use. Just don't call it models.py
because it will confuse Django admin. Call it something meaningful to your application: foo.py
. This foo.py
manipulates the real things that underpin your application -- not necessarily a Django Model.model
subclass.
您可以——通常是这样——定义您的视图使用的自己的“模型”模块。不要叫它模型。因为它会混淆Django admin。可以将其称为对应用程序有意义的东西:foo.py。这foo。py操纵支撑应用程序的真实事物——不一定是Django模型。模型子类。
Django MVC does not require a database mapping. It does explicitly expect that the module named models.py
has a database mapping in it. So, use an empty models.py
if you have no actual database mapping.
Django MVC不需要数据库映射。它明确地期望模块命名为models。py中有一个数据库映射。所以,使用一个空模型。如果没有实际的数据库映射,则为py。
Your views.py
can use
你的观点。py可以使用
import foo
def index( request ):
objects = foo.somelistofobjects()
*etc.*
Django allows you to easily work with no database mapping. Your model can easily be anything. Just don't call it models.py
.
Django允许在没有数据库映射的情况下轻松工作。你的模型可以是任何东西。不要叫它model .py。
Edit.
编辑。
Are Views registered with Models? No.
视图是否与模型注册?不。
On update to the Model by the Controller the Views get notified? No.
当控制器更新模型时,视图会被通知吗?不。
Is the Model strictly the data respresentation as this is really MVP? Yes.
模型是否严格的数据响应,因为这是真正的MVP?是的。
Read the Django docs. It's simple.
Django文档阅读。这很简单。
Web Request -> URL mapping -> View function -> Template -> Response.
Web请求-> URL映射->视图功能->模板->响应。
The model can be used by the view function. The model can be a database mapping, or it can be any other thing.
该模型可由视图函数使用。模型可以是数据库映射,也可以是其他任何东西。