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

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

问题描述

我刚刚通过Django开始工作,真的很喜欢它,但是我有一个问题,我不知道什么是典型的解决方法。



假设我有一个视图,当一些复杂的Python对象被更新时,它应该被更新,但这个对象不是由数据库驱动的,比如说它是由AJAX调用或直接由用户驱动的。 p>

此代码在哪里?应该还是在models.py ????

解决方案

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



您还应该有一个 models.py 文件,以使Django的管理员开心。 models.py 文件名很重要,而且比使用各种管理命令预期的文件更容易,空文件比较简单。



模型 - 通常 - 不必映射到数据库。 模型 - 作为MVC设计的一般组件 - 可以是任何东西。



您可以 - 并且经常做 - 定义您自己的模型模块您的意见使用。 只需不要调用它,因为它会混淆Django管理员。调用它对您的应用程序有意义: foo.py 。这个 foo.py 操纵了支持你的应用程序的真正的东西 - 不一定是Django Model.model 子类。 / p>

Django MVC不需要数据库映射。 它明确地期望名为 models.py 的模块在其中具有数据库映射。所以,使用一个空的模型.py 如果您没有实际的数据库映射。



您的 views.py 可以使用

  import foo 

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

Django允许您轻松地使用数据库映射。你的模型很容易就可以了。只要不要称之为 models.py






编辑



是否注册了模型?否。



在控制器更新模型时,视图会收到通知?否。



模型是否严格地是数据表示,因为这真的是MVP?是的。



阅读Django文档。这很简单。



Web请求 - > URL映射 - >查看功能 - >模板 - >响应。



该模型可以被视图功能使用。该模型可以是数据库映射,也可以是其他任何东西。


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.

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

解决方案

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

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.

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

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 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.

Your views.py can use

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.


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.

Read the Django docs. It's simple.

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天全站免登陆