默认情况下一个django app的所有模型都保存在一个叫models.py的文件中、这样事实是不方便管理的;

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

 

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

rm -rf models.py

 

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

mkdir models
touch models/__init__.py

 

三、例子:

1、

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

2、

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

3、

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

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>

 

----

相关文章:

  • 2021-12-20
  • 2021-11-22
  • 2021-11-23
  • 2021-11-30
  • 2021-09-01
  • 2021-08-28
  • 2021-11-19
  • 2021-11-19
猜你喜欢
  • 2021-08-22
  • 2021-06-25
  • 2021-06-23
  • 2021-08-20
  • 2021-08-31
  • 2021-10-13
相关资源
相似解决方案