This is the 9th day of my participation in Gwen Challenge

field

The most important and only necessary part of the model is the database’s field definition, which is defined in class attributes.

  • When defining field names, care should be taken to avoid using names that conflict with the model API, such as clean, Save, DELETE, and so on

The field type

Each Field in the model should be an instance of a Field class to do the following:

  • Specify the database data type (e.g., INTEGER, VARCHAR, TEXT)
  • Specify the default HTML view to use when rendering form fields (e.g.<input type="text"><select>).
  • Contains basic validation functionality for Django backend and auto-generated forms

Django comes with dozens of built-in field types, or you can customize them if Django’s built-in types don’t meet your requirements

Field options

Each type of field requires specific parameters. For example, CharField needs to receive a max_length parameter that specifies the number of bytes used to store VARCHAR data in the database. Some of the optional parameters are generic and can be used for any field type. Here are some common parameters that are often used:

  • null

If set to True, Django sets the field to NULL in the database when it is NULL. The default is False.

  • blank

If set to True, this field is allowed to be null. The default is False.

Note that this option is different from NULL, which is only a database-level setting, whereas blank is concerned with form validation. If a field is set to blank=True, the value of that field is allowed to be null when receiving data for form validation, and blank=False is not allowed to be null.

  • choices

A series of binary groups that are used as options for this field. If binary groups are provided, the default form widget is a selection box rather than a standard text field and will limit the options given. A new migration is created every time the order of choices changes.

# The first value of each binary will be stored in the database, while the second value will only be used for display in the form.
from django.db import models

class Person(models.Model) :
    SHIRT_SIZES = (
        ('S'.'Small'),
        ('M'.'Medium'),
        ('L'.'Large'),
    )
    name = models.CharField(max_length=60)
    shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)

For a model instance, to get the corresponding second value in the field binary, use the get_FOO_display() method. Such as:
p = Person(name="Fred Flintstone", shirt_size="L")
p.save()
p.shirt_size
# 'L'
p.get_shirt_size_display()
# 'Large'
Copy the code
  • default

The default value for this field. It can be a value or a callable object, or if it is a callable object, it will be called every time the model is instantiated.

  • help_text

Additional Help text is displayed with the form control. Even if your fields are not used in forms, they can be useful for generating documents.

  • primary_key

If set to True, this field is set as the primary key of the model.

  • In a model, if the primary_key=True option is not set for any of the fields. Django automatically adds an IntegerField and sets it to the primary key,
  • The primary key field is readable only. If you change the primary key of a model instance and save it, it is equivalent to creating a new model instance.
from django.db import models

class Fruit(models.Model) :
    name = models.CharField(max_length=100, primary_key=True)

fruit = Fruit.objects.create(name='Apple')
fruit.name = 'Pear'
fruit.save()
Fruit.objects.values_list('name', flat=True)
<QuerySet ['Apple'.'Pear'] >Copy the code
  • unique

If set to True, the value of this field must remain unique throughout the table.

  • verbose_name

The human-readable name corresponding to the field, used in Django Admin. If no detailed name is given, Django automatically creates it using the field’s attribute name and converts the underscore to a space.