您所在的位置:小祥子 » 编程 » Python » 正文

Django web 开发入门教程 (二)

时间:2015-08-11 编辑:小麦粉 来源:CnBlogs

创建 models

 每一个通过Django编写的应用都包含一个遵循特定约定 Python包。 Django 包含一个自动生成基本目录结构的工具, 所有你不必创建目录,只需关注编写代码。

Projects vs. apps

What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a 
database of public records or a simple poll app. A project is a collection of configuration and apps for a particular Web site. A project
can contain multiple apps. An app can be in multiple projects.
注: apps 是一个Web 应用,可以是一个网络日志系统,一个简单的投票系统。 一个project 是配置和应用程序(apps) 的集合。 project 可以包含多个 apps。 一个apps 可以纯在于多个project中。

要创建apps, 请确定当前目录和manage.py在同一目录中,并键入以下指令:

$ python manage.py startapp polls
polls/
    __init__.py
    admin.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

编写一个数据库Web app 的第一步就是定义你的models (实质上就是数据库的元数据设计)。

Philosophy

A model is the single, definitive source of truth about your data. It contains the essential fields and behaviors of the data you’re storing. 
Django follows the DRY Principle. The goal is to define your data model in one place and automatically derive things from it. This includes the migrations - unlike in Ruby On Rails, for example, migrations are entirely derived from your models file, and are
essentially just a history that Django can roll through to update your database schema to match your current models.

在我们的简单的poll app中, 我们将创建两个models:QuestionChoice。 Question有一个question和publication date。 Choice有两个字段:choice文本和选票统计。 每一个Choice 都关联一个Question。

这些概念都是由简单的python class进行表示。 如下编辑 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)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

上面的代码非常直观。 每个model 由一个django.db.models.Model 的子类表示。 每个model 有一些类变量, 每个变量代表数据库的字段。

Each field is represented by an instance of a Field class – e.g., CharField for character fields and DateTimeField for datetimes. This tells 
Django what type of data each field holds. The name of each Field instance (e.g. question_text or pub_date) is the field’s name, in machine-friendly format. You’ll use this value in your
Python code, and your database will use it as the column name. You can use an optional first positional argument to a Field to designate a human-readable name. That’s used in a couple of introspective parts of
Django, and it doubles as documentation. If this field isn’t provided, Django will use the machine-readable name. In this example, we’ve only defined a
human-readable name for Question.pub_date. For all other fields in this model, the field’s machine-readable name will suffice as its human-readable name. Some Field classes have required arguments. CharField, for example, requires that you give it a max_length. That’s used not only in the
database schema, but in validation, as we’ll soon see. A Field can also have various optional arguments; in this case, we’ve set the default value of votes to 0. Finally, note a relationship is defined, using ForeignKey. That tells Django each Choice is related to a single Question. Django supports all the
common database relationships: many-to-one, many-to-many and one-to-one.

激活 models

这么一小块代码为Django 提供了大量的信息。 通过它,Django 可以做下面这些事情:

  • 创建数据库结构(schema, CREATE TABLE statements)
  • 创建一个访问QuestionChoice 对象的 Python 的数据库 API

但是, 首先需要做的事情就是告诉我们的project, polls app 已将安装。

Philosophy

Django apps are “pluggable”: You can use an app in multiple projects, and you can distribute apps, because they don’t have to be tied to a given Django installation.

编辑文件 mysite/settings.py ,并修改 INSTALLED_APPS 设置,包含字符串 'polls'.如下所示:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)
关键词:入门