---
title: "Django 1.0 中文文档-----字段"
author: "剑事"
date: "2009-02-13T20:40:16Z"
tags: ["Django", "F#"]
categories: ["编程语言"]
slug: "django-1-0"
draft: false
lastmod: "2009-02-25T20:33:11Z"
id: 332558
url: "https://jfy3d.iteye.com/blog/332558"
---

模型字段参考

 

字段选项

 

null

 

Field.`null`

`` 

`如果为True，django会默认字段值为null,null默认为False。`

`注意空字符值会被存储为空字符而不是null。仅null=True 用于非字符类型字段，如整型，布尔，日期。如果你想允许form里空值还需要设置 blank=True， null 只和数据库存储有关。`

`` 

``blank

 

Field.blank

 

如果为True，字段值可以为空白，默认是False

 

 

choices

 

Field.`choices`

 

一个跌送的元组列表，用作一个选择字段。

如果提供值，django管理工具会用select来替代文本字段。

选择列表如下：

```
YEAR_IN_SCHOOL_CHOICES = (
    ('FR', 'Freshman'),
    ('SO', 'Sophomore'),
    ('JR', 'Junior'),
    ('SR', 'Senior'),
    ('GR', 'Graduate'),
)
```

 

元组的第一个元素会被存储，第二个是可读的选项名。

选择列表可如下同模型定义到一起

```
class Foo(models.Model):
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
```

 

也可定义到模型外部

```
GENDER_CHOICES = (
    ('M', 'Male'),
    ('F', 'Female'),
)
class Foo(models.Model):
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
```

 

还可以按组定义

```
MEDIA_CHOICES = (
    ('Audio', (
            ('vinyl', 'Vinyl'),
            ('cd', 'CD'),
        )
    ),
    ('Video', (
            ('vhs', 'VHS Tape'),
            ('dvd', 'DVD'),
        )
    ),
    ('unknown', 'Unknown'),
)
```

 

db\_column

 

Field.`db_column`

``

数据库表列名，如果没有提供，默认使用字段名。  

 

db\_index

 

Field.`db_index`

``

`如果为True,django-admin.py sqlindexes <sqlindexes> 将会创建索引。`

``

`db_tablespace`

 

Field.`db_tablespace`

 

设置表的命名空间

 

default

 

Field.`db_tablespace`

 

设置字段默认值

 

editable

 

Field.`editable`

``

`设置字段的可编辑性，默认为True`

``

`help_text`

``

`Field.help_text`

``

设置字段的说明文字。

 

primary\_key

 

Field.`primary_key`

``

`如果为True，这个字段会被设置为主键。`

``

`unique`

``

`Field.unique`

``

`如果为True，这个字段值会被约束为唯一不重复。` 

 

 

unique\_for\_date

 

Field.`unique_for_date`

``

`设置到一个DateField 或 DateTimeField 字段名，两个字段值会被约束唯一。`

``

`例如 title 字段设置has unique_for_date="pub_date"，那么django就不允许有两条title和pub_date值同时相等的记录。`

``

`unique_for_month`

`Field.unique_for_month`

 

如unique\_for\_date，只是约束月份

 

unique\_for\_year

 

Field.unique\_for\_year

 

如unique\_for\_date，只是约束年份

 

 

字段类型

 

**AutoField**

**class AutoField(\*\*options)**

 

一个自动递增的整型字段，会被默认设置为primary key，如果你没有提供值，它会自动增加。

 

**BooleanField**

**class BooleanField(\*\*options)**

 

布尔字段,管理工具里会自动将其描述为checkbox。

 

**CharField**

**class CharField(max\_length=None\[, \*\*options])**

 

字符字段，CharField有一个必填参数,`CharField.` `max_length，字符的最大长度，django会根据这个参数校验。`

 

**CommaSeparatedIntegerField**

**class CommaSeparatedIntegerField(max\_length=None\[, \*\*options])**

 

一个用逗号分割整数的字段，和charfield基本属性类似。

 

**DateField**

**class DateField(\[auto\_now=False, auto\_now\_add=False, \*\*options])**

 

日期字段 有几个参数

 

`DateField.` `auto_now 自动更新为当前时间，每次对模型中的字段修改，这个字段都会自动更新问当前世嘉相当于lastmodify时间印章`

`DateField.` `auto_now_add` 模型保存的时候，如果为指定指端值，会默认为当前时间。

 

**DateTimeField**

**class DateTimeField(\[auto\_now=False, auto\_now\_add=False, \*\*options])**

 

日期时间字段，参数类似 `DateField`

 

### `DecimalField`

New in Django 1.0.

**class `DecimalField` (*max\_digits=None* , *decimal\_places=None* \[ , *\*\*options* ] )**

小数字段，需要两个参数：

`DecimalField.` `max_digits`

最大位数

`DecimalField.` `decimal_places`

小数位数

### `EmailField`

**class `EmailField` (\[ *max\_length=75* , *\*\*options* ] )**

电子邮件字段，会校验电子邮件输入格式的有效性。

### `FileField`

**class `FileField` (*upload\_to=None* \[ , *max\_length=100* , *\*\*options* ] )**

文件上传字段，有个必填字段

`FileField.` `upload_to 文件的存储路径`

`FileField.` `storage`

New in Django 1.0

文件临时保存路径

**FilePathField**

**class FilePathField(path=None\[, match=None, recursive=False, max\_length=100, \*\*options])**

 

选择指定目录按限制规则选择文件，有几个参数：

FilePathField.`path 指定绝对路径`

`FilePathField.match 匹配规则`

`FilePathField.recursive 是否包含子目录`

`` 

`` 

```
FilePathField(path="/home/images", match="foo.*", recursive=True)
```

``

` `

``

`FloatField`

``

`class FloatField([**options])`

``

` `

``

`浮点型字段`

``

` `

``

 

 

**ImageField**

**class ImageField(upload\_to=None\[, height\_field=None, width\_field=None, max\_length=100, \*\*options])**

 

图像字段 

 

 

 

 

ImageField.`height_field 通过模型的其他字段指定图像高。`

`ImageField.width_field 通过模型的其他字段指定图宽。`

`` 

`IntegerField`

`class IntegerField([**options])`

 

整型字段

 

**IPAddressField**

**class IPAddressField(\[\*\*options])**

 

IP类型字段

**NullBooleanField**

**class NullBooleanField(\[\*\*options])**

 

类似[`BooleanField`](/admin/blogs/332559/#django.db.models.BooleanField "django.db.models.BooleanField")，但是允许为null。

 

**PositiveIntegerField**

**class PositiveIntegerField(\[\*\*options])**

 

正整数字段

 

**PositiveSmallIntegerField**

 

**class PositiveSmallIntegerField(\[\*\*options])**

 

正小整型字段

**SlugField**

 

**class SlugField(\[max\_length=50, \*\*options])**

 

是一个报纸术语. slug 是某个东西的小小标记(短签), 只包含字母,数字,下划线和连字符.它们通常用于URLs 

 

**SmallIntegerField**

**class SmallIntegerField(\[\*\*options])**

 

小整型字段

 

**TextField**

**class TextField(\[\*\*options])**

 

文本字段，管理工具会描述为textarea

 

**TimeField**

**class TimeField(\[auto\_now=False, auto\_now\_add=False, \*\*options])**

 

时间字段

 

**URLField**

**class URLField(\[verify\_exists=True, max\_length=200, \*\*options])**

 

URL 字段

 

URLField.verify\_exists 验证地址是否有效

 

 

**XMLField**

**class XMLField(schema\_path=None\[, \*\*options])**

 

XML字符字段

schema\_path  用来校验文本的 RelaxNG_ schema 的文件系统路径

 

**ForeignKey**

**class ForeignKey(othermodel\[, \*\*options])**

 

多对一关系字段，需要定位到一个模型，如果是自身递归关系models.ForeignKey('self').

 

```
class Car(models.Model):
    manufacturer = models.ForeignKey('Manufacturer')
    # ...

class Manufacturer(models.Model):
    # ...
```

 

 

ForeignKey.`limit_choices_to 限制关联的条件`

 

``

```
limit_choices_to = {'pub_date__lte': datetime.now}
```

``

` `

``

 

 

ForeignKey.`related_name 关联字段名`

`ForeignKey.to_field 关联目标字段`

 

### `ManyToManyField`

**class `ManyToManyField`(*othermodel*\[, *\*\*options*])**

多对多字段

ManyToManyField.`related_name 关联字段名`

`ManyToManyFields.limit_choices_to 限制关联的条件`

`ManyToManyFields.symmetrical 用于自己对自己的多对多`

`ManyToManyFields.through 使用中间模型`

`ManyToManyField.db_table 多对多关系表`

 

### `OneToOneField`

**class `OneToOneField`(*othermodel*\[, *parent\_link=False*, *\*\*options*])**

 一对一字段

OneToOneField.`parent_link 父级链接`