1. 创建数据模型
polls/models.py
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question,on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
2. 将app载入项目中
INSTALLED_APPS=
[
'dbmanage.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
3. 自动创建对应的sql语句 —— $python manage.py makemigrations polls
4. 自动创建数据对象的表 ——$python manage.py migrate
5. 创建admin用户 —— $python manage.py createsuperuser
6. 将数据对象加入admin的管理中
polls/admin.py
from django.contrib import admin
from . models import Question
admin.site.register(Question)