创建模型
创建模型
在 Django 里写一个数据库驱动的 Web 应用的第一步是定义模型 - 也就是数据库结构设计和附加的其它元数据。
在这个简单的投票应用中,需要创建两个模型:问题 Question 和选项 Choice。Question 模型包括问题描述和发布时间。Choice 模型有两个字段,选项描述和当前得票数。每个选项属于一个问题。
这些概念可以通过一个简单的 Python 类来描述。按照下面的例子来编辑 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)
注:
1.每个模型有一些类变量,它们都表示模型里的一个数据库字段。
2.我们使用 ForeignKey 定义了一个关系。这将告诉 Django,每个 Choice 对象都关联到一个 Question 对象。Django 支持所有常用的数据库关系:多对一ForeignKey 、多对多ManyToManyField和一对一OneToOneField。
3.如果是跨文件设置外键,首先引入模型,然后引入外键,如:
from django.db import models
from geography.models import ZipCode
class Restaurant(models.Model):
zip_code = models.ForeignKey(ZipCode,
on_delete=models.SET_NULL,
blank=True,
null=True)
激活模型
首先得把 polls 应用安装到我们的项目里,为了在我们的工程中包含这个应用,我们需要在配置类 INSTALLED_APPS中添加设置:
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
现在你的 Django 项目会包含 polls 应用。接着运行下面的命令:
$ python manage.py makemigrations polls
通过运行 makemigrations 命令,Django 会检测你对模型文件的修改(在这种情况下,你已经取得了新的),并且把修改的部分储存为一次 迁移。
现在,再次运行 migrate 命令,在数据库里创建新定义的模型的数据表:
$ python manage.py migrate
这个 migrate 命令选中所有还没有执行过的迁移(Django 通过在数据库中创建一个特殊的表 django_migrations 来跟踪执行过哪些迁移)并应用在数据库上 - 也就是将你对模型的更改同步到数据库结构上。
迁移是非常强大的功能,它能让你在开发过程中持续的改变数据库结构而不需要重新删除和创建表 - 它专注于使数据库平滑升级而不会丢失数据。我们会在后面的教程中更加深入的学习这部分内容,现在,你只需要记住,改变模型需要这三步:
1.编辑 models.py 文件,改变模型。
2.运行 python manage.py makemigrations 为模型的改变生成迁移文件。
3.运行 python manage.py migrate 来应用数据库迁移。
Field options
常用字段
null
If True, Django will store empty values as NULL in the database.
Default is False.
blank
If True, the field is allowed to be blank. Default is False.
Note that this is different than null.
null is purely database-related, whereas
blank is validation-related. If a field has
blank=True, form validation will
allow entry of an empty value. If a field has blank=False, the field will be required.
choices
An iterable (e.g., a list or tuple) of 2-tuples to use as choices for
this field. If this is given, the default form widget will be a select box
instead of the standard text field and will limit choices to the choices
given.
A choices list looks like this:
YEAR_IN_SCHOOL_CHOICES = (
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
)
The first element in each tuple is the value that will be stored in the
database. The second element is displayed by the field's form widget.
Given a model instance, the display value for a field with choices can
be accessed using the get_FOO_display()
method. For example:
from django.db import models
class Person(models.Model):
SHIRT_SIZES = (
('S', 'Small'),
('M', 'Medium'),
('L', 'Large'),
)
name = models.CharField(max_length=60)
shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)
>>> p = Person(name="Fred Flintstone", shirt_size="L")
>>> p.save()
>>> p.shirt_size
'L'
>>> p.get_shirt_size_display()
'Large'
default
The default value for the field. This can be a value or a callable
object. If callable it will be called every time a new object is
created.
help_text
Extra "help" text to be displayed with the form widget. It's useful for
documentation even if your field isn't used on a form.
primary_key
If True, this field is the primary key for the model.
If you don't specify primary_key=True for
any fields in your model, Django will automatically add an
IntegerField to hold the primary key, so you don't need to set
primary_key=True on any of your fields
unless you want to override the default primary-key behavior. For more,
see Automatic primary key fields.
The primary key field is read-only. If you change the value of the primary
key on an existing object and then save it, a new object will be created
alongside the old one. For example:
from django.db import models
class Fruit(models.Model):
name = models.CharField(max_length=100, primary_key=True)
>>> fruit = Fruit.objects.create(name='Apple')
>>> fruit.name = 'Pear'
>>> fruit.save()
>>> Fruit.objects.values_list('name', flat=True)
<QuerySet ['Apple', 'Pear']>
unique
If True, this field must be unique throughout the table.