03 - Django 定义模型
作者: 张启卫
时间: 2017年4月13号
功能:
- 使用Django创建表
- 理解模型的概念
03 - 数据库设置
打开mysite/settings.py
python manage.py migrate #installed_APPs
设置数据库
设置Time-zone
第二步:定义models
myblog/polls/models.py
from django.db import models
# Create your models here.
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)
learnning-log/learnning-logs/models.py
from django.db import models
# Create your models here.
# 创建一个Topic的类
# 包含text 与 date_added属性
# 包含__str__方法,返回text中字符串
class Topic(models.Model):
"""用户学习的主题"""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""返回模型的字符串表示"""
return self.text
第三步: 激活模型
mysite/settings.py
# Application definition
# 这是一个元组,告诉Django项目是由哪些应用程序组成的
INSTALLED_APPS = (
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘blog’,
‘polls.apps.PollsConfig’,
‘learning_logs’
)
#修改数据库Polls
python manage.py makemigrations polls
#查看sql语句
python manage.py sqlmigrate polls 0001
#检查
python manage.py check
# 让项目迁移
python manage.py migrate
(ll_env) David@10:~/02_project/learning-log$ python manage.py makemigrations learning_logs
Migrations for 'learning_logs':
learning_logs/migrations/0001_initial.py
- Create model Topic
(ll_env) David@10:~/02_project/learning-log$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, learning_logs, sessions
Running migrations:
Applying learning_logs.0001_initial... OK
小结:
修改数据库步骤:
第一步:定义models
第二步: python manage.py makemigrations
第三步: python manage.py migrate
python manage.py shell
or
python
import django
django.setup()
>>> from polls.models import Question, Choice # Import the model classes we just wrote.
# No questions are in the system yet.
>>> Question.objects.all()
<QuerySet []>
# Create a new Question.
# Support for time zones is enabled in the default settings file, so
# Django expects a datetime with tzinfo for pub_date. Use timezone.now()
# instead of datetime.datetime.now() and it will do the right thing.
>>> from django.utils import timezone
>>> q = Question(question_text=“What’s new?”, pub_date=timezone.now())
# Save the object into the database. You have to call save() explicitly.
>>> q.save()
# Now it has an ID. Note that this might say “1L” instead of “1”, depending
# on which database you’re using. That’s no biggie; it just means your
# database backend prefers to return integers as Python long integer
# objects.
>>> q.id
1
# Access model field values via Python attributes.
>>> q.question_text
“What’s new?”
>>> q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
# Change values by changing the attributes, then calling save().
>>> q.question_text = “What’s up?”
>>> q.save()
# objects.all() displays all the questions in the database.
>>> Question.objects.all()
<QuerySet [<Question: Question object>]>
Django 管理员
第一步:创建一个管理员用户
python manage.py createsuperuser
http://127.0.0.1:8000/admin
第二步:让app在admin管理中可用
polls/admin.py
from django.contrib import admin
from .models import Question
# Register your models here.
admin.site.register(Question)
理解模型的概念
每一个模型都是django.db.models.Model的子类
每个属性都表示数据库的一个字段

浙公网安备 33010602011771号