Using django Models 批注

原文:https://docs.djangoproject.com/en/1.6/topics/db/models/

★ 一个 widget 对应一个 HTML 表单输入元素。 widget 负责HTML的渲染,从 GET/POST 字典里抽出和此 widget 相符的数据。

Models

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

★ model 代表用户数据,包含数据其必要的域和行为。一般来说,一个模型映射到一个对应的表。

The basics:

  • Each model is a Python class that subclasses django.db.models.Model★ model 是一个 Python 类,继承 django.db.models.Model
  • Each attribute of the model represents a database field.  ★ model 里的属性代表 database field
  • With all of this, Django gives you an automatically-generated database-access API; see Making queries. ★ Django 提供了访问数据库的 API 

Quick example

This example model defines a Person, which has a first_name and last_name:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

first_name and last_name are fields of the model. Each field is specified as a class attribute, and each attribute maps to a database column.

The above Person model would create a database table like this:

CREATE TABLE myapp_person (
    "id" serial NOT NULL PRIMARY KEY,
    "first_name" varchar(30) NOT NULL,
    "last_name" varchar(30) NOT NULL
);

Some technical notes:

  • The name of the table, myapp_person, is automatically derived from some model metadata but can be overridden. See Table names for more details.★ 在内置类Meta里,用db_table改变默认的表名
  • An id field is added automatically, but this behavior can be overridden. See Automatic primary key fields. ★ 若未指定主键 Django 提供默认的 id = models.AutoField(primary_key=True) 
  • The CREATE TABLE SQL in this example is formatted using PostgreSQL syntax, but it’s worth noting Django uses SQL tailored to the database backend specified in your settings file. ★ TODO

Using models

Once you have defined your models, you need to tell Django you’re going to use those models. Do this by editing your settings file and changing the INSTALLED_APPS setting to add the name of the module that contains your models.py.

★ 在 settings 文件的 INSTALLED_APPS 里设置 Django 应用,告诉Django你要使用这个应用下面的模型。

For example, if the models for your application live in the module myapp.models (the package structure that is created for an application by the manage.py startapp script), INSTALLED_APPS should read, in part:

INSTALLED_APPS = (
    #...
    'myapp',
    #...
)

When you add new apps to INSTALLED_APPS, be sure to run manage.py syncdb.

Fields

The most important part of a model – and the only required part of a model – is the list of database fields it defines. Fields are specified by class attributes. Be careful not to choose field names that conflict with the models API like cleansaveor delete.

★ Fields 定义了db table fields。不要和 models API 里的名字冲突。

Example:

from django.db import models

class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    instrument = models.CharField(max_length=100)

class Album(models.Model):
    artist = models.ForeignKey(Musician)
    name = models.CharField(max_length=100)
    release_date = models.DateField()
    num_stars = models.IntegerField()

Field types

Each field in your model should be an instance of the appropriate Field class. Django uses the field class types to determine a few things:  ★ 讲了 Field 类都做了哪些事 

  • The database column type (e.g. INTEGERVARCHAR). ★ model field 代表了db的字段
  • The default HTML widget to use when rendering a form field (e.g.  type="text">