V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
prasanta
V2EX  ›  Python

关于 Django 更合理的项目结构讨论

  •  
  •   prasanta · 2017-05-12 16:01:10 +08:00 · 2652 次点击
    这是一个创建于 2513 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 API 接口开发过程中,我使用了 Django Rest Framework. 使用 Django 原始的项目结构进行开发时感觉不够流畅,不知道大家有没有同感

    为此, 我自己设计了一个比较舒适的结构进行 API 接口的开发. 我参考了许多 Django 项目以及其它语言类似的框架的项目结构.

    大家可以看看哪里可以一起优化的.

    目录结构(博客项目举例)

    {app.name}
       api/
            __init__.py
            urls.py
            tasks.py
            models.py
            migrations/
                __init__.py
            docs/
                users.md
                blogs.md
                catagries.md
                comments.md
                tags.md
            tests/
                __init__.py
                test_users.py
                test_blogs.py
                test_comments.py
                test_tags.py
                test_catagries.py
            resources/
                __init__.py
                users.py
                blogs.py
                catagries.py
                tags.py
                comments.py
    
        config/
            __init.py
            common.py
            dev.py
            prod.py
    
        celery.py
        manage.py
        wsgi.py
        fabfile.py
        .gitignore
        mkdocs.yml
        requirements.txt
    
    
    • 其中 resources 文件夹下每个文件代表一种资源,如 users.py 代表与 user 有关的所有接口都在这儿.
    • 一个 users.py 中同时包含 Serializer,ViewSet,Permission,而不是像之前那样全部分开到 permissions.py,serializers.py,views.py 中.
    • 我个人主观感觉这样逻辑更清晰,写起来更舒适,.

    不知道大家有什么开发?

    7 条回复    2017-05-13 00:10:40 +08:00
    ansheng
        1
    ansheng  
       2017-05-12 17:35:56 +08:00
    你这个,,一个文件,即写了 view 又写了 Serializer,ViewSet,Permission 等,个人认为,晦涩难懂,代码文件太长,还是因人而意吧,弱弱的问一句,tests 你放哪儿?
    ansheng
        2
    ansheng  
       2017-05-12 17:36:46 +08:00
    test..... 不合理
    ```bash
    tests/
    __init__.py
    test_users.py
    test_blogs.py
    test_comments.py
    test_tags.py
    test_catagries.py
    ```
    既然都已经有了 tests 就不用再加 test_了吧,个人感觉
    prasanta
        3
    prasanta  
    OP
       2017-05-12 17:40:15 +08:00
    @ansheng

    1. 如果分好模块后,users.py 里面的代码量是会比之前全在 views.py 中少的
    2. 加 test_是一种约定.
    chuanqirenwu
        4
    chuanqirenwu  
       2017-05-12 18:09:33 +08:00
    主要还是看对象。
    ansheng
        5
    ansheng  
       2017-05-12 18:22:39 +08:00
    @prasanta 我觉得少的只会是一些 import 或者 from
    mapleray
        6
    mapleray  
       2017-05-12 19:48:39 +08:00 via iPhone
    <two scoops of Django > 里面有专讲结构,看看感觉会对你有很大帮助
    prasanta
        7
    prasanta  
    OP
       2017-05-13 00:10:40 +08:00
    @mapleray 看过了,不太适合 API 接口开发的情况
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2751 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:35 · PVG 20:35 · LAX 05:35 · JFK 08:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.