BigAutoField – Django 模型
BigAutoField 是一个 64 位整数,与 AutoField 非常相似,只是它保证适合从1到9223372036854775807的数字。可以使用以下语法创建 BigAutoField,
id = models.BigAutoField(primary_key=True, **options)
这是一个自动递增的主键,就像 AutoField 一样。
Django 模型 BigAutoField 解释
使用示例说明 BigAutoField。考虑一个名为 geeksforgeeks 的项目,它有一个名为 geeks 的应用程序。
Refer to the following articles to check how to create a project and an app in Django.
- How to Create a Basic Project using MVT in Django?
- How to Create an App in Django ?
在极客应用的models.py文件中输入以下代码。
Python3
from django.db import models
from django.db.models import Model
# Create your models here.
class GeeksModel(Model):
big_id = models.BigAutoField(primary_key = True)
Python3
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'geeks',
]
Python3
# 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 =[
('big_id',
models.BigAutoField(auto_created = True,
primary_key = True,
serialize = False,
verbose_name ='ID'
)),
],
),
]
将极客应用添加到 INSTALLED_APPS
Python3
# 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 的文件
Python3
# 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 =[
('big_id',
models.BigAutoField(auto_created = True,
primary_key = True,
serialize = False,
verbose_name ='ID'
)),
],
),
]
因此,当您在项目上运行 makemigrations 时,会创建一个在该模型的每个实例上自动递增的 big_id BigAutoField 。它是为名为 GeeksModel 的模型创建的表的主键。
Possible Error
since a Django model can have at most one autofield, BigAutoField must be created at the very first migration with primary_key=True attribute otherwise it will raise multiple errors.
“AssertionError: A model can’t have more than one AutoField.”
If you get this error try deleting the recent migrations and adding primary_key=True to the field. Run Migrations again to solve the problem.
如果我们从管理服务器创建这个空模型的对象。我们可以看到 id 字段在每个创建的实例上自动递增。
字段选项
字段选项是赋予每个字段的参数,用于应用某些约束或将特定特征赋予特定字段。例如,向 BigAutoField 添加参数 primary_key=True 将使其成为关系数据库中该表的主键。
以下是 BigAutofield 可以使用的选项和属性。Field Options Description Null If True, Django will store empty values as NULL in the database. Default is False. Blank If True, the field is allowed to be blank. Default is False. db_index If True, a database index will be created for this field. db_tablespace The name of the database tablespace to use for this field’s index if this field is indexed. The default is the project’s DEFAULT_INDEX_TABLESPACE setting, if set, or the db_tablespace of the model if any. If the backend doesn’t support tablespaces for indexes, this option is ignored. db_column The name of the database column to use for this field. If this isn’t given, Django will use the field’s name.
Default The 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_text Extra “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_key If True, this field is the primary key for the model. editable If 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_messages The 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_text Extra “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_name A 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.
validators A list of validators to run for this field. See the validators documentation for more information.
Unique If True, this field must be unique throughout the table.