非数据库驱动模型的 Django MVC 模式? [英] Django MVC pattern for non database driven models?

查看:24
本文介绍了非数据库驱动模型的 Django MVC 模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习 Django,到目前为止真的很喜欢它,但是我遇到了一个问题,我不确定解决它的典型方法是什么.

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.

假设我有一个视图,它应该在一些复杂的 Python 对象更新时更新,但这个对象不是由数据库驱动的,比如说它是由 AJAX 调用驱动的,或者直接由用户或其他东西驱动.

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.

这段代码去哪儿了?它还应该在models.py中吗????

Where does this code go? Should it still go in models.py????

推荐答案

您的 models.py 可以(有时是)为空.您没有义务拥有映射到数据库的模型.

Your models.py can be (and sometimes is) empty. You are not obligated to have a model which maps to a database.

您仍然应该有一个 models.py 文件,以使 Django 的管理员满意.models.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.

模型"——一般来说——不必映射到数据库.模型"——作为 MVC 设计的通用组件——可以是任何东西.

The "model" -- in general -- does not have to map to a database. The "model" -- as a general component of MVC design -- can be anything.

您可以——而且经常这样做——定义您自己的视图使用的模型"模块.不要将其称为 models.py,因为它会使 Django 管理员感到困惑. 将其称为对您的应用程序有意义的东西:foo.py.这个 foo.py 操作支撑您的应用程序的真实事物——不一定是 Django Model.model 子类.

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 MVC 不需要数据库映射.它确实明确要求名为 models.py 的模块中有一个数据库映射.因此,如果有,请使用空的 models.py没有实际的数据库映射.

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.

你的views.py可以使用

import foo

def index( request ):
    objects = foo.somelistofobjects()
    *etc.*

Django 允许您在没有数据库映射的情况下轻松工作.您的模型很容易成为任何东西.只是不要叫它models.py.

Django allows you to easily work with no database mapping. Your model can easily be anything. Just don't call it models.py.

编辑.

视图是否注册到模型中?号

Are Views registered with Models? No.

控制器更新模型时,视图会收到通知吗?号

On update to the Model by the Controller the Views get notified? No.

模型是否严格来说是数据表示,因为这真的是 MVP?是的.

Is the Model strictly the data respresentation as this is really MVP? Yes.

阅读 Django 文档.很简单.

Read the Django docs. It's simple.

Web Request -> URL mapping -> View function -> Template -> Response.

Web Request -> URL mapping -> View function -> Template -> Response.

模型可以被视图函数使用.模型可以是数据库映射,也可以是其他任何东西.

The model can be used by the view function. The model can be a database mapping, or it can be any other thing.

这篇关于非数据库驱动模型的 Django MVC 模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆