Conteúdo do Curso
Django ORM Ninja: Advanced Techniques for Developers
Django ORM Ninja: Advanced Techniques for Developers
Field Data Types
In this chapter, we will consider the most useful data types for fields, with other types described in the official documentation.
Field data type | Python type | Description |
CharField | str | Text-based values |
IntegerField | int | Positive and negative integer field |
TextField | str | A large text field without any limitations in character number. |
DecimalField | float (decimal) | It is a fixed-precision decimal number. More precise than just float numbers in Python. This field typically includes parameters like max_digits and decimal_places |
DateField | date | A date, presented as datetime.date instance in Python |
DateTimeField | datetime | A date and time, presented as datetime.datetime instance in Python |
EmailField | str | String that checks that value is a valid email address. |
ImageField | ... | Validates that the uploaded object is a valid image. |
The most common field options are:
- max_length (it is the required argument for the CharField field);
- null (if True, it will be possible to store empty values as NULL. Default is False);
- blank (if True, the field is allowed to be blank. Default is False);
- default (You can pass the default value for the field. For example, often used
date_created = models.DateTimeField(default=datetime.utcnow())
the current time will be stored as a value); - primary_key (usually used for the id field);
- unique (if True, this field must be unique throughout the table. Useful for nickname field, for example);
- choices (the default form widget will be a select box instead of a standard text field and will limit choices).
Here is an example of how to use the choices field:
Obrigado pelo seu feedback!