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

What is a primary key?

In a database, a primary key is a unique identifier for each record and is used to distinguish different records. Therefore, if there is a primary key, it cannot be repeated.

Can there be no primary key?

A native database can have no primary key, but in Django, models always have a primary key, and Django automatically adds one even if you don’t specify one yourself. Each model needs to have a field with primary_key=True set (either explicitly or automatically by Django).

Manually set the primary key

  • If you want to specify the primary key yourself, set the parameters in the field you want to set the primary key toprimary_key=True.
  • primary_key=Truemeansnull=Falseunique=TrueThat is, the primary key must be non-empty and unique.
  • An object can have only one primary key.
from django.db import models

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

Automatically set the primary key

By default, Django gives each model an auto-incrementing primary key. If field.primary_key is explicitly set (primary_key=True), id columns (attributes) will not be automatically added to the table (model).

  • Types of automatic primary keys:

    • AutoField(Default type) : oneIntegerFieldAutomatically increments based on available ids. You usually don’t need to use it directly; If not specified, the primary key field is automatically added to your model.
    • BigAutoField: a 64-bit integer, andAutoFieldVery similar, but guaranteed to fit from 1 to 9223372036854775807
  • Global Settings Default primary key type: DEFAULT_AUTO_FIELD is set in configuration (setting.py)

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
# Django 3.2 new features
Copy the code
  • Local Settings Default primary key type: in applicationapps.pyModule SettingsAppConfig.default_auto_field(Default is global DEFAULT_AUTO_FIELD)
# anthology/apps.py apps.py module in the app
from rock_n_roll.apps import RockNRollConfig

class JazzManoucheConfig(RockNRollConfig) :
   default_auto_field = 'django.db.models.AutoField'
   # Django 3.2 new features


# anthology/settings.py configuration file setting
INSTALLED_APPS = [
   'anthology.apps.JazzManoucheConfig'.#...
]
Copy the code

Field Remarks Nameverbose_name

With the exception of ForeignKey fields (ForeignKey, ManyToManyField, neToOneField), any field type accepts the first optional positional parameter verbose_name. If this parameter is not specified, Django automatically uses the field’s property name as the value. And convert underscores to Spaces.

Name: "person's first name"
first_name = models.CharField("person's first name", max_length=30)

Name: "first name":
first_name = models.CharField(max_length=30)
Copy the code

ForeignKey fields (ForeignKey, ManyToManyField, neToOneField) can also be set with a note name: the first parameter is passed the class name, followed by the verbose_name parameter.

poll = models.ForeignKey(
    Poll,    The first parameter is the class name of the model
    on_delete=models.CASCADE,
    verbose_name="the related poll".# add verbose_name to verbose_name
)
sites = models.ManyToManyField(Site, verbose_name="list of sites")
place = models.OneToOneField(
    Place,
    on_delete=models.CASCADE,
    verbose_name="related place".)Copy the code

The convention is not to capitalize verbose_name; Djanog will automatically capitalize the initial if necessary.