The following sections cover a nonexhaustive list of the field types in models.
The model fields are those that will be saved in the database. Depending on the database system selected, the type field may be different depending on the database used.
The types are specified with their options in the following manner:
Type (option1 = example_data, option2 = example_data) [information]
Fields presented in this section are numeric fields such as integers and decimals:
The descriptions of the options are as follows:
This section contains the types of fields that contain strings:
CharField (max_length = 250)
TextField (max_length = 250)
: This field has the distinction of being presented as a<textarea>
tag in the Django formsEmailField (max_length = 250)
: This field isCharField
that contains an e-mail validator for Django forms
The description of the option is as follows:
max_length
: This sets the maximum number of characters that compose the string
This section contains the types of fields that contain temporal data:
DateField (auto_now = false
,auto_now_add = true)
DateTimeField (auto_now = false
,auto_now_add = true)
TimeField (auto_now = false
,auto_now_add = true)
The descriptions of the options are as follows:
This section contains the types of fields that do not belong to the previous categories:
BooleanField()
FileField: (upload_to = "path", max_length="250")
: This field is used to store files on the serverImageField(upload_to = "path", max_length="250", height_field =height_img, width_field= width_img)
: This field corresponds toFileField
but imparts special treatment to images such as storing the image's height and width
The descriptions of the options are as follows:
Upload_to
: This defines the folder that will store the files corresponding to this field.max_length
: TheFileField
andImageField
fields are actually text fields that store the path and name of the uploaded file.height_field
andwidth_field
: These take an integer field of the model as an argument. This field is used to store the size of the image.
This section contains the types of fields that define the relationships between models:
ForeignKey (model, related_name = "foreign_key_for_dev", to_field="field_name", limit_choices_to=dict_or_Q, on_delete=)
OneToOneField (model, related_name = "foreign_key_for_dev", to_field="field_name", limit_choices_to=dict_or_Q, on_delete=)
ManyToManyField (model, related_name = "foreign_key_for_dev", to_field="field_name", limit_choices_to=dict_or_Q, on_delete=)
The descriptions of the options are as follows:
model
: Here, you must specify the name of the model class you want to use.related_name
: This allows you to name the relationship. It is essential when multiple relationships to the same model exist.to_field
: This defines a relationship to a specific field of the model. By default, Django creates a relationship to the primary key.on_delete
: The database action on the removal of a field can beCASCADE
,PROTECT
,SET_NULL
,SET_DEFAULT
, andDO_NOTHING
.limit_choices_to
: This defines the queryset that restricts records for the relationship.
The model meta attributes are to be defined in a meta class in the model in the following way:
class Product(models.Model): name = models.CharField() class Meta: verbose_name = "product"
The following attributes are used to define information about the model in which they are placed:
The following options are common to all the fields of a model:
null
: This enables the null value for the field and makes an optional relationship if this option is defined on a relationship field.unique
: This defines a field that does not contain duplicates.verbose_name
: This defines a field name that is readable by a human. Do not put a capital letter first; Django will do it automatically.choices
: This defines the number of possible choices for the field.db_column
: This sets the name of the field created in the database.