1.创建一个django项目,叫djangodb_demo
2.使用mysql数据库
2.在djangodb_demo项目下面再创建一个叫web的app
3.在web下新建一个models.py的文件
from django.db import models
class UserInfo(models.Model):
username = models.CharField(maxlength=50)
password = models.CharField(maxlength=50)
同时,需要在settings.py中将web加入到项目中
目录结构为
4.在djangodb_demo项目的settings.py文件中配置连接数据库的信息
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': djangodb_demo,
'USER': 'root',
'PASSWORD': '123456',
'HOST': '10.10.20.134',
'PORT': '',
}
}
engine:你所使用的数据库类型
name:django项目在mysql中的库
user:连接库的用户名
5.在mysql中建一个叫“djangodb.demo”的库
注意:django不能创建库,但是能创建表
6.使用django创建表
Operations to perform:
Synchronize unmigrated apps: staticfiles, web, messages
Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Creating table web_userinfo
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying sessions.0001_initial... OK
Finished "/root/eclipse_data/djangodb_demo/manage.py migrate" execution.
执行成功!
可以看到除了在models.py中有一个UserInfo的表,其他都是django自己创建的!
再来看看django把我们的userinfo表创建成了什么样
转载于:https://blog.51cto.com/fangniu/1744526