主键字段上的serialize = False是什么意思? [英] What means the serialize=False on Primary-key field?
问题描述
我在Django文档和源代码中没有找到主键字段上 serialize = False
的原因.设置它是否有特殊原因?
I didn't found in the Django docs and Source Code the reason for serialize=False
on primary key fields. Is there a special reason to set it?
谢谢
推荐答案
Azd325,
听起来很简单,该字段不会成为序列化对象的一部分.
it is as simple as it sounds, this field will not be part of the serialized object..
尽管,我想您的问题与正在迁移的模型有关,并且模型的生成ID带有serialize = False,对吗?例如在此处中.确实是一个文档,因为创建内在ID是Django的引擎技巧,因为您决定不为对象声明显式ID.
Although, I guess your question concerns to models that are being migrated and have a generated ID with serialize=False, right? such as in here.. There isn't really a documentation on this because it is Django's engine trick to create an intrinsic ID since you decided not to declare an explicit ID for your object..
我刚刚做了一些测试
创建一个没有明确ID的模型
Create a model without an explicit ID
class Model1Test(models.Model):
justafield = models.CharField(max_length=1000)
迁移结果
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='Model1Test',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('justafield', models.CharField(max_length=1000)),
],
),
]
数据库级脚本
CREATE TABLE public.module1_model1test
(
id integer NOT NULL DEFAULT nextval('module1_model1test_id_seq'::regclass),
justafield character varying(1000) NOT NULL,
CONSTRAINT module1_model1test_pkey PRIMARY KEY (id)
)
从 Quassnoi :
- 您需要您的表可以与某些东西连接
- 如果要对表进行集群,则需要某种主键
- 如果您的表设计不需要主键,请重新考虑您的设计:很可能您丢失了某些东西.为什么要保留相同的记录?
这篇关于主键字段上的serialize = False是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!