📜  AutoField – Django 模型

📅  最后修改于: 2022-05-13 01:55:34.077000             🧑  作者: Mango

AutoField – Django 模型

根据文档,AutoField 是一个根据可用 ID 自动递增的 IntegerField。通常不需要直接使用它,因为如果您没有另外指定,主键字段将自动添加到您的模型中。

默认情况下,Django 为每个模型提供以下字段:

id = models.AutoField(primary_key=True, **options)

这是一个自动递增的主键。即使模型没有任何字段,也会创建一个名为 id 的默认字段。

Django 模型自动字段说明

使用示例说明 AutoField。考虑一个名为geeks的项目,它有一个名为geeksforgeeks的应用程序。

极客应用的models.py文件中输入以下代码。

from django.db import models
from django.db.models import Model
# Create your models here.
  
class GeeksModel(Model):
    pass

将极客应用添加到INSTALLED_APPS

# Application definition
  
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'geeks',
]

现在,当我们从终端运行makemigrations命令时,

Python manage.py makemigrations

将在geeks目录中创建一个名为 migrations 的新文件夹,其中包含一个名为0001_initial.py的文件

# Generated by Django 2.2.5 on 2019-09-25 06:00
  
from django.db import migrations, models
  
class Migration(migrations.Migration):
  
    initial = True
  
    dependencies = [
    ]
  
    operations = [
        migrations.CreateModel(
            name ='GeeksModel',
            fields =[
                ('id', 
                  models.AutoField(auto_created = True,
                  primary_key = True,
                  serialize = False, 
                  verbose_name ='ID'
                )),
            ],
        ),
    ]

因此,当您在项目上运行 makemigrations 时,默认情况下会创建一个在该模型的每个实例上自动递增id AutoField。它是为名为GeeksModel的模型创建的表的主键。

如果我们从管理服务器创建这个空模型的对象。我们可以看到 id 字段在每个创建的实例上自动递增。
Django 模型自动字段

字段选项

字段选项是赋予每个字段的参数,用于应用某些约束或将特定特征赋予特定字段。例如,将参数primary_key=True添加到 AutoField 将使其成为关系数据库中该表的主键。
以下是 Autofield 可以使用的选项和属性。

Field OptionsDescription
NullIf True, Django will store empty values as NULL in the database. Default is False.
BlankIf True, the field is allowed to be blank. Default is False.
db_columnThe name of the database column to use for this field. If this isn’t given, Django will use the field’s name.
DefaultThe default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
help_textExtra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.
primary_keyIf True, this field is the primary key for the model.
editableIf False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True.
error_messagesThe error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.
help_textExtra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.
verbose_nameA human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces.
validatorsA list of validators to run for this field. See the validators documentation for more information.
UniqueIf True, this field must be unique throughout the table.