Django开发者群  - 讨论区

标题:半尾Django模型实例参考翻译作业帖

2010年07月27日 星期二 22:25

Model instance reference¶模型实例参考

This document describes the details of the Model API. It builds on the material presented in the model and database query guides, so you’ll probably want to read and understand those documents before reading this one.

本文档描述了模型API接口的细节。它建立在模型和数据库查询指南的资料基础上,因此你可能想在阅读本文档前阅读和理解这些资料。

Throughout this reference we’ll use the example weblog models presented in the database query guide.

贯穿本参考我们还将使用数据库查询指南中weblog模型例子。

Creating objects¶创建对象

To create a new instance of a model, just instantiate it like any other Python class:创建一个模型的新实例,只需像其他Python类一样实例化它:

class Model(**kwargs)¶

The keyword arguments are simply the names of the fields you’ve defined on your model. Note that instantiating a model in no way touches your database; for that, you need to save().

关键字参数只是你已经定义在你模型上的字段名称。注意实例化模型并没有接触数据库;如果要,你需要save()



Validating objects¶验证对象

New in Django 1.2: Please, see the release notes

There are three steps involved in validating a model:有三个步骤用来验证一个模型

Validate the model fields 验证模型的字段

Validate the model as a whole 验证模型整体

Validate the field uniqueness 验证字段唯一性



All three steps are performed when you call by a model’s full_clean() method.当你调用模型的full_clean()方法时,所有三个步骤都执行。

When you use a ModelForm, the call to is_valid() will perform these validation steps for all the fields that are included on the form. (See the ModelForm documentation for more information.) You should only need to call a model’s full_clean() method if you plan to handle validation errors yourself, or if you have excluded fields from the ModelForm that require validation.

当你使用ModelForm,调用is_valid()将对所有包含在表单中的字段执行这些验证步骤。(参考MOdelForm文档获取更多信息)如果你计划自行处理验证错误你需要调用模型的full_clean()方法,或者你排除了ModelForum中需要验证的字段)

Model.full_clean(exclude=None)¶

This method calls Model.clean_fields(), Model.clean(), and Model.validate_unique(), in that order and raises a ValidationError that has a message_dict attribute containing errors from all three stages.

这个方法按照Model.clean_fields(),Model.clean(),Model.Validate_unique()的顺序调用并且引发一个ValidationError异常,它拥有一个message_dict属性包含了全部三个步骤的异常。

The optional exclude argument can be used to provide a list of field names that can be excluded from validation and cleaning. ModelForm uses this argument to exclude fields that aren’t present on your form from being validated since any errors raised could not be corrected by the user.

选择性的exclude参数可以用来提供一个可以被排除验证和清除的字段名称列表。ModelForum使用这个参数把不表现在你的表单上的字段从被验证字段里排除出来,因为任何引起的异常用户都无法更正。

2010年07月28日 星期三 10:37

翻译得非常不错。

有个小建议:

simply翻译为“简单来说”更好些

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号