开发者

django app organization

开发者 https://www.devze.com 2023-01-03 00:42 出处:网络
I have been reading some django tutorial and it seems like all the view functions have to go in a file called \"views.py\" and all the models go in \"models.py\". I fear that I might end up with a lot

I have been reading some django tutorial and it seems like all the view functions have to go in a file called "views.py" and all the models go in "models.py". I fear that I might end up with a lot of view functions in my view.py file and the same is the case with models.py.

Is my understanding of django apps correct?

Django apps lets us separate common functionality into different apps and keep th开发者_StackOverflow中文版e file size of views and models to a minimum? For example: My project can contain an app for recipes (create, update, view, and search) and a friend app, the comments app, and so on.

Can I still move some of my view functions to a different file? So I only have the CRUD in one single file?


First, large files are pretty common in python. Python is not java, which has one class per file, rather one module per file.

Next, views, even as the standard used, is a python module. A module need not be a single file. It can be a directory containing many files, and __init__.py

And then, views.py is only a convention. You, the application programmer are referring to it, and django itself doesn't refer anywhere. So, you are free to put it in as many files and refer appropriate functions to be handed over, the request to, in the urls.py


They don't have to go in views.py. They have to be referenced there.

views.py can include other files. So, if you feel the need, you can create other files in one app that contain your view functions and just include them in views.py.

The same applies to models.py.

Django apps lets us separate common functionality into different apps and keep the file size of views and models to a minimum? For example: My project can contain an app for recipes (create, update, view, and search) and a friend app, the comments app, and so on.

I don't know about the "to a minimum" part - some apps are just big in views, others big in models. You should strive to partition things well, but sometimes there is just a lot of code. But other than that, this is a fair summary of Django apps, yes.


I also very much dislike long files.

Of course what you read in the other answers is true, but I exploit some very nifty python equivalence:

views.py

and

views/__init__.py

are pretty much functionally equal - by that I mean that if the both contain def my_view() then

from views import my_view

will work the same in both cases!

From there it's easy to structure your long files into smaller ones, yet keeping the naming convention that every django developer is used to:

views/__init__.py
views/largemodel_view.py

then in __init__.py don't forget to import the views from largemodel_view.py.

With large applications I do the same with models though you must remember to set the Meta.app_name:

class MyModel(models.Model):
    ...

    class Meta:
        app_name = 'yourappname'

because django will not pick it up magically otherwise for the Admin (but it will still load it, thanks to Python!)

so my apps usually end up looking something like:

project/settings/__init__.py
                /..othersettings..
       /app_1/models/__init__.py
                    /...
             /views/__init__.py
                   /...
             /templates/
             /static/
             urls.py
       /urls.py

etc.

though of course there's no limit (urls could be split too, etc.etc.)

0

精彩评论

暂无评论...
验证码 换一张
取 消