• Aucun résultat trouvé

Common Field Attributes

Dans le document Pro Django (Page 73-76)

Different field types will have different attributes according to their needs, but there are several attributes that are common across most built- in Django fields. These can be used to generically access various details of fields, and by association, the values and behaviors they’re meant to interface with. Note that there are more attributes used internally than those listed here, but these are the most useful and stable, and will provide the greatest value to applications looking to work with fields.

The descriptions listed here are how Django itself uses these attributes, and how develop-ers will expect them to behave. Other applications will likely find use for them as well, to control certain types of behaviors, so the following descriptions will help illustrate their intended usage.

Some applications may find uses that are slightly different from what Django itself expects to use them for, but the general semantics of the values should remain intact. Remember that developers will build their expectations for these values based on how Django itself behaves, and third- party applications should avoid violating these expectations.

฀ ฀]ppj]ia—The name of the attribute on model instances where the database- related value is stored. This is typically the same as the j]ia attribute, for simple cases where the value from the database is stored directly on the model. In other cases, it’s more appropriate to expose a more complex object, such as another model instance, to other code when the actual field name is accessed. For those cases, ]ppj]ia and j]ia will be different, with the attribute referenced by j]ia being the complex object, while the attribute referenced by ]ppj]ia contains the raw data required to create it.

฀ ฀^h]jg—A Boolean value indicating whether the field must have a value supplied when using a form generated automatically based on the model. This is purely validation- related behavior; the jqhh attribute controls whether a model can actually be saved in the data-base without a value for the given field.

฀ ฀_dke_ao—A sequence of 2- tuples indicating the valid choices for the field. The first item in each tuple is the actual value that would be stored in the database if selected, while the second item is the text that will be displayed to the user for that value.

฀ ฀_khqij—The name of the database column that will be used to hold the field’s value.

This will either match `^[_khqij, if the field declared its database column explicitly, or will have been generated automatically, based on the field’s name. Normally, this can be ignored, since Django manages the database interaction directly, but some applica-tions may have need to communicate directly with the database or interface with some other database adapter that will need this information.

฀ ฀`^[_khqij—The name explicitly supplied as the database column name for the field’s values. This is different from _khqij in that `^[_khqij refers to what the model itself declares, rather than what will actually be used. This will only have a value if the model field specified its `^[_khqij argument explicitly; it will be Jkja otherwise.

฀ ฀`^[ej`at—A Boolean indicating whether the field was declared to have an index cre-ated for it in the database. This only indicates whether the field was configured to instruct Django to create the index. Other indexes may have been added directly in the database itself, which won’t necessarily be reflected in the value of this attribute.

฀ ฀`^[p]^haol]_a—The tablespace directive indicating where the field’s data will be stored. Currently only supported for the Oracle backend, the format of its contents will depend on which database backend is in place. It will always have a string value, defaulting to the value of the @AB=QHP[EJ@AT[P=>HAOL=?A setting if not set explicitly.

฀ ฀`ab]qhp—The default value for the field, to be used if no value has yet been supplied to the field itself. In addition to being inserted into the database in such a case, this value will be used as the field’s initial value for any forms generated based on the model. The type of value stored in this attribute will be whatever native Python data type the field is intended to interact with, such as a string or an integer.

฀ ฀a`ep]^ha—A Boolean indicating whether the field should be presented to users for editing when generating forms based on the model. This doesn’t make the field itself read- only from within Python so this is far from a guarantee that the field won’t be edited. It’s simply a directive to control the default behavior of forms, though other applications can—and should—use it to control other behaviors as well, if they provide editing capabilities.

฀ ฀ailpu[opnejco[]hhksa`—A Boolean indicating whether the field allows an empty string as a possible value. This isn’t an option specified as the configuration of a specific field instance, but is rather defined in the field’s class itself. Many fields, such as ?d]nBeah`

and Ai]ehBeah`, treat empty strings separately from Jkja, so this attribute allows back-ends to decide how to handle empty strings for databases, such as Oracle, that might otherwise lose that distinction.

฀ ฀dahl[patp—The informative text provided in the field definition, to be displayed to users when the field is presented for editing. This will be passed in for forms that are generated based on the model, such as the provided admin interface.

฀ ฀i]t[hajcpd—The maximum length the field’s value can contain. Most string- based fields, such as ?d]nBeah` and Ai]ehBeah`, use this to limit the length of string content, both in form fields and the underlying database column. Other field types, such as EjpacanBeah` and @]paBeah`, simply ignore it, as it has no meaning in those cases.

฀ ฀j]ia—The name of the field, as defined when assigning the field to the model. This is set as part of the _kjpne^qpa[pk[_h]oo$% process, to maintain DRY by avoiding having to type the name twice. This will be the name of the attribute where the field’s native Python value will be assigned and retrieved. Contrast this with ]ppj]ia, which stores the raw data necessary to populate j]ia. Often, the two values will be the same, but the distinction is important to understand, for cases where they’re different.

฀ ฀jqhh—A Boolean indicating whether the field can be committed to the database with-out a value assigned. This primarily controls how the underlying database column is created, but some applications may find other uses, as long the semantics remain the same.

฀ ฀lnei]nu[gau—A Boolean indicating whether the field should be used as the primary key for the database table. In addition to instructing the database to generate the primary key index, Django uses this indicator to determine which field’s value to use when looking up specific instances, such as related objects through foreign key relationships.

See the section “Primary Key Fields” earlier in this chapter for details on the [iap]*lg shortcut for determining which field has this value set to Pnqa.

฀ ฀nah—In the case of fields that relate one model to another, this will be a special object describing the various aspects of that relationship. For all non- relationship field types, this will be set to Jkja.

฀ ฀oane]heva—A Boolean indicating whether the field should be included when model instances are serialized using the serialization framework.3

฀ ฀qjemqa—A Boolean indicating the field must be unique among all instances of the model. This is primarily used to create the proper constraints in the database to enforce this condition, but it can also be used by applications. For instance, a content editing application that provides detailed feedback about whether the user- entered values are valid for the model can also take this into account when making that determination.

฀ ฀qjemqa[bkn[`]pa—The name of a date- related field, such as a @]paBeah` or @]paPeiaBeah`, for which this value should be unique. This is essentially like qjemqa, except that the con-straint is limited to records that occur on the same date, according to the field referenced by this attribute. This can’t be enforced at the database level, so Django manages the constraint manually, as should any other applications that need to provide detailed infor-mation about whether a given object can be committed to the database.

฀ ฀qjemqa[bkn[ikjpd—Like qjemqa[bkn[`]pa, except that the uniqueness is only required for objects that occur within the same month, according to the date- related field refer-enced by the name contained by this attribute.

฀ ฀qjemqa[bkn[ua]n—Like qjemqa[bkn[`]pa, except that the uniqueness is only required for objects that occur within the same year, according to the date- related field refer-enced by the name contained by this attribute.

3. dppl6+lnk`f]jck*_ki+oane]hev]pekj+

฀ ฀ran^koa[j]ia—The full name of the field, in plain English, to be displayed to users.

Django’s documentation recommends that this begin with a lowercase letter, so that applications can capitalize it as necessary. If an application needs this value capital-ized, be sure to use the _]lbenop$% utility method, described in Chapter 9.

Dans le document Pro Django (Page 73-76)