views:

562

answers:

2

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

+3  A: 

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.

S.Lott
Your answer may or may not be correct, I haven't used Django, and its been 2 years since I've used Python. But this is what I absolutely hate about what web frameworks have done to the concept of MVC. There is no ironclad requirement that the model data in the MVC paradigm comes from the database.
George Jempty
The model does *not* have to come from a database. We do it in our Django applications. Some of our apps have models that are not in the database.
S.Lott
Fair enough but apparently you have to work in a way that is not standard in Django in order to do so. And of course I've seen this with other frameworks too. I think they've all done a dis-service to impressionable, less experience web developers by introducing the notion of "model classes". Most of the time these are actually *entity* classes. I don't even agree that there should be something such as a model class, the model is at an even higher level of abstraction. A model facade perhaps, but model classes, no. Anyway, it's my pet peeve and I guess I'm an MVC purist or something.
George Jempty
I agree with you guys, coming with quite a bit of MVC experience as a design pattern I was a little bit thrown by what exactly Django was trying to accomplish by forcing it's models to map to the database.
DevDevDev
@George Jempty: False. You do not have to work in a way that is not standard in Django. There's no disservice. The model can be anything. Anything at all. They offer database mappings, you can decline them. Just don't use `models.py` as the module, or the admin will be confused.
S.Lott
@DevDevDev: I have no idea what you're saying. Django `models.py` contain database mappings. You do not have to use them. Your model can be anything.
S.Lott
@S.Lott so as long as I define my model in a file other than models.py and inherit from models.Model I will get all of the MVC plumbing for me but not all of the database mapping?
DevDevDev
@S.Lott Maybe I misunderstand if this is really MVC is the traditional sense. Are Views registered with Models and on update to the Model by the Controller the Views get notified, or is the Model strictly the data respresentation as this is really MVP?
DevDevDev
@DevDevDev: What? If you inherit from `models.Model`, then your "model" is database driven and must be in `models.py`. If you don't want to have a database mapping, I'm not sure what you're doing, but you should *not* be messing with `models.Model`.
S.Lott
Sorry didn't mean to open this can of worms. However I'm not sure why S.Lott would say "False" to my assertion that without a database you have to work in a non-standard way, but then goes on to say "Just don't use models.py as the module, or the admin will be confused." I contend that this is clearly then non-standard for Django.
George Jempty
@George Jempty: I guess your threshold for non-standard is very low. Leaving the `models.py` file empty doesn't strike me as non-standard. That file is used in a special way. Leave it empty to avoid odd things happening. Seems perfectly standard to me.
S.Lott
A: 

I see the date on this is from last year, but I'll post anyway.

If you don't want to use a database in django, should you still set a database engine in the settings file?

Thanks,

Todd Matsumoto