E:\paython学习\setup\mysql-5.6.24-win32\bin>mysqld -install Service successfully installed. E:\paython学习\setup\mysql-5.6.24-win32\bin>net start mysql MySQL 服务正在启动 ......... MySQL 服务已经启动成功。 E:\paython学习\setup\mysql-5.6.24-win32\bin>mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.24 MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database poll default charset utf8; Query OK, 1 row affected (0.02 sec) mysql>
安装 MySQL-python-1.2.4b4.win32-py2.7.exe 在linux上也要安装这个驱动 yum install python-mysql
E:\paython学习\pycharm>django-admin startproject mysite E:\paython学习\pycharm>cd mysite E:\paython学习\pycharm\mysite>django-admin startapp polls
mysite.settings.py INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls', //增加polls应用 ) DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', //更换数据库为mysql 'NAME': 'poll', 'HOST': '127.0.0.1', 'USER': 'root', 'PASSWORD': '', 'PORT': '3306', } }
from django.db import models from django.utils import timezone import datetime # Create your models here. class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date_published') def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1) def __unicode__(self): return self.question_text class Choice(models.Model): question = models.ForeignKey(Question) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) def __unicode__(self): return self.choice_text
E:\paython学习\pycharm\mysite>python manage.py makemigrations polls //建立迁移文件 Migrations for 'polls': 0001_initial.py: - Create model Choice - Create model Question - Add field question to choice E:\paython学习\pycharm\mysite>python manage.py migrate //迁移到数据库 Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: admin, contenttypes, polls, auth, sessions Synchronizing apps without migrations: Creating tables... 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 polls.0001_initial... OK Applying sessions.0001_initial... OK
mysql> show tables; Empty set (0.00 sec) mysql> show tables; +----------------------------+ | Tables_in_poll | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | django_admin_log | | django_content_type | | django_migrations | | django_session | | polls_choice | | polls_question | +----------------------------+ 12 rows in set (0.00 sec) 可见的确写到数据库中了
E:\paython学习\pycharm\mysite>python manage.py shell Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on wi 32 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from django.utils import timezone >>> from polls.models import Question, Choice >>> Question.objects.all() [] >>> from django.utils import timezone >>> q = Question(question_text="what's new",pub_date=timezone.now()) >>> q.save() >>> q.id 1L >>> q.pub_date datetime.datetime(2015, 7, 10, 13, 21, 21, 781000, tzinfo=<UTC>) >>> q.question_text = "what's up" >>> q.save()>>> Question.objects.all() [<Question: what's up>] >>> Question.objects.filter(id=1) [<Question: what's up>] >>> current_year = timezone.now().year >>> Question.objects.get(pub_date__year=current_year) <Question: what's up> >>> q = Question.objects.get(pk=1) >>> q <Question: what's up> >>> q.choice_set.all() [] >>> q.choice_set.create(choice_text='NOT much',votes=0) <Choice: NOT much> >>> q.choice_set.create(choice_text='the sky', votes=0) <Choice: the sky> >>> c = q.choice_set.create(choice_text='just hacking again',votes=0) >>> c.question <Question: what's up> >>> q.choice_set.all() [<Choice: NOT much>, <Choice: the sky>, <Choice: just hacking again>] >>> q.choice_set.count <bound method RelatedManager.count of <django.db.models.fields.related.RelatedM nager object at 0x015F9030>> >>> q.choice_set.count() 3 >>> c = q.choice_set.filter(choice_text__startswith='just hacking') >>> c.delete()
这边介绍了几种模型的创建和选择的方法,之后在视图逻辑中都会用的到,如何把数据写入数据库,如何读等等
question是choice的外键
一个question有好多个choice
可以通过question来访问choice,也可以通过question来创建choice,因为他们之间有约束关系