您的位置:首页 > 数据库

django -- 用包来组织数据库模型

2017-12-02 17:03 369 查看
默认情况下一个django app的所有模型都保存在一个叫models.py的文件中、这样事实是不方便管理的;

通过包来组织模型是比较方便的。

一、第一步:删除models.py:

rm -rf models.py


二、第二步:创建models目录、并增加标记文件__init__.py:

mkdir models
touch models/__init__.py


三、例子:

1、



2、



3、



4、

python3 manage.py shell
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django
>>> django.setup()
>>> from polls.models import Person,Student
>>> s=Student()
>>> s.name='welson'
>>> s.sid=1
>>> s
<Student: I am welson my sid is 1>


----
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: