[django]document_translation
ImageField
- class ImageField(**kwargs)
- Default widget: ClearableFileInput
- Empty value: None
- to: An UploadedFile object that wraps the file content and file name into a single object.normalize
- Validates that file data has been bound to the form, and that the file is of an image format understood by PIL.
- Error message keys: required, invalid, missing, empty, invalid_image
Using an ImageField require
When you use an ImageField on a form, you must also remember to bind the file data to the form.
默认插件:ClearableFileInput
空值:None
标准来自: 包含file content和file name的单一uploadedfile对象
文件数据被绑定在form后,并且数据能被PIL识别时触发验证
错误关键信息 :required,invalid,missing,empty,invalid_image
使用Image Field须知:
确保Python Imaging Libaray已安装
当你在form模型中使用imagefield时,你必须记得在form POST中绑定文件数据.
=======================================================
What happens when you save?
When you save an object, Django performs the following steps:
Emit a pre-save signal. The signal django.db.models.signals.pre_save is sent, allowing any functions listening for that signal to take some customized action.
Pre-process the data. Each field on the object is asked to perform any automated data modification that the field may need to perform.
Most fields do no pre-processing -- the field data is kept as-is. Pre-processing is only used on fields that have special behavior. For example, if your model has a DateField with auto_now=True, the pre-save phase will alter the data in the object to ensure that the date field contains the current date stamp. (Our documentation doesn't yet include a list of all the fields with this "special behavior.")
Prepare the data for the database. Each field is asked to provide its current value in a data type that can be written to the database.
Most fields require no data preparation. Simple data types, such as integers and strings, are 'ready to write' as a Python object. However, more complex data types often require some modification.
For example, DateFields use a Python datetime object to store data. Databases don't store datetime objects, so the field value must be converted into an ISO-compliant date string for insertion into the database.
Insert the data into the database. The pre-processed, prepared data is then composed into an SQL statement for insertion into the database.
Emit a post-save signal. The signal django.db.models.signals.post_save is sent, allowing any functions listening for that signal to take some customized action.
在你保存的时候发生了什么
当你保存一个对象,django 的执行将会遵循以下步骤:
1、发出预保存信号。信号django.db.models.signals.pre_save 已经发出,允许所有函数监听这个信号进行一些自定义行为。
2、预处理数据。对象上的各个字段 将会被自动修改,如果它们需要被修改的话。
大多数字段不需要预处理——简单类型如integers和string是'ready to write'的Python对象。然而,更多复杂的数据类型常常需要一些修改。
比如说DataFields使用Python的datetime对象去存储数据,数据库不存储datatime对象,所以该字段值在插入数据库时需要转换为被ISO允许的数据字 符串。
3、将数据插入数据库。预处理后,呈递的数据已经被构成为SQL 声明将要插入数据库。
4、发出保存信号。信号django.db.models.signals.post_save已经发出,允许所有已经监听的函数执行一些自定义行为。
How Django knows to UPDATE vs. INSERT
You may have noticed Django database objects use the same save() method for creating and changing objects. Django abstracts the need to use INSERT or UPDATE SQL statements. Specifically, when you call save(), Django follows this algorithm:
- If the object's primary key attribute is set to a value that evaluates to True (i.e., a value other than None or the empty string), Django executes a SELECT query to determine whether a record with the given primary key already exists.
- If the record with the given primary key does already exist, Django executes an UPDATE query.
- If the object's primary key attribute is not set, or if it's set but a record doesn't exist, Django executes an INSERT.
The one gotcha here is that you should be careful not to specify a primary-key value explicitly when saving new objects, if you cannot guarantee the primary-key value is unused. For more on this nuance, see Explicitly specifying auto-primary-key values above and Forcing an INSERT or UPDATE below.
Django是怎么分辨UPDATE与INSERT呢
你应试注意到django数据库对象在实现数据对象的创建与变更时使用的是同一个save()方法。django 理论上需要
是使用INSERT和UPDATE的SQL语法。可以明确,当你调用save()时候,django遵循这个规则:
如果对象的主键属性被设置了值则判定为TRUE,(换言之,这个值不是None或empty string)Django 执行
一个SELECT 查询 并根据查询记判断 已经设置了的主键值是否存在
如果含有主键值的查询记录是存在的话,django 执行一个UPDATE查询
如果对象的主键属性没有被设置,或者已经设置了但是记录中不存在的话,django 执行INSERT
至此你应该明确知道当你保存新对象的时候,如果你不能确保主键值是否正被使用,你必须小心的指定一个明确的主键值 ,。
更多的差异,请查看.....
Forcing an INSERT or UPDATE
In some rare circumstances, it's necessary to be able to force the save() method to perform an SQL INSERT and not fall back to doing an UPDATE. Or vice-versa: update, if possible, but not insert a new row. In these cases you can pass theforce_insert=True or force_update=True parameters to the save() method. Passing both parameters is an error, since you cannot both insert and update at the same time.
It should be very rare that you'll need to use these parameters. Django will almost always do the right thing and trying to override that will lead to errors that are difficult to track down. This feature is for advanced use only.
强制INSERT或UPDATE
在一些罕见的情况下,它需要允许save()方法去强制实施一个SQL INSERT 并且不能回滚到UPDATE,反之如果需要更新,则不能插入新行。这种情况下你可以输入force_insert=True或force_update=True 参数给save()方法。同时传入两个方法是错误的,因为你不能同时插入和更新(同一对象)
需要你使用这些参数的情况非常少见,DJANGO会一如既往的做正确的事,并且尝试掩盖那些难于追寻的错误。这些都仅仅是为了高级使用。