1. ?創(chuàng)建數(shù)據(jù)模型
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載入項(xiàng)目中
INSTALLED_APPS= ? ?
? ? [
? ? ? ? 'dbmanage.apps.PollsConfig',
? ? ? ? 'django.contrib.admin',
? ? ? ? 'django.contrib.auth',
? ? ? ? 'django.contrib.contenttypes',
? ? ? ? 'django.contrib.sessions',
? ? ? ? 'django.contrib.messages',
? ? ? ? 'django.contrib.staticfiles',
? ? ]
3. 自動(dòng)創(chuàng)建對應(yīng)的sql語句 —— $python manage.py makemigrations polls
4. 自動(dòng)創(chuàng)建數(shù)據(jù)對象的表 ——$python manage.py migrate
5. 創(chuàng)建admin用戶 —— $python manage.py createsuperuser
6. 將數(shù)據(jù)對象加入admin的管理中
polls/admin.py
from django.contrib import admin
from . models import Question
admin.site.register(Question)