14

I couldn't find the reason for serialize=False being set on primary key fields in either the Django docs or the source code. Is there a special reason to set it?

Thanks

Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
Azd325
  • 5,752
  • 5
  • 34
  • 57

2 Answers2

9

Azd325,

it is as simple as it sounds, this field will not be part of the serialized object..

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..

Some additions of tests I just made

Create a model without an explicit ID

class Model1Test(models.Model):
    justafield = models.CharField(max_length=1000)

Migration results

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)),
            ],
        ),
    ]

Database-level script

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)
)

Some good reasons to do that from Quassnoi:

  1. You need your table to be joinable on something
  2. If you want your table to be clustered, you need some kind of a primary key
  3. If your table design does not need a primary key, rethink your design: most probably, you are missing something. Why keep identical records?
Community
  • 1
  • 1
bobleujr
  • 1,179
  • 1
  • 8
  • 23
  • 2
    This answer didn't make sense to me - and it's also not ok that there's no documentation on this. I need to understand it's purpose so I know whether or not to modify it, and when to use it. – odigity May 19 '22 at 23:17
  • "this field will not be part of the serialized object" - some docs on serialization: https://docs.djangoproject.com/en/4.2/topics/serialization/. But where exactly serialized objects are used is still not obvious to me. – dfrankow Aug 12 '23 at 16:36
1

This question bothered me too. I wasn't able to find any documentation that answers the question "why", but after thinking this through here's where I landed.

When you're serializing an object, you're communicating your information "out". Frequently over the network, or maybe another "consuming program" on the same server(s).

If the primary key was auto generated by Django, it's not really "information". It's an aid to organizing (reading, writing) that data within Django. There's no benefit to serializing it and sending it over to a different system (which may have its own way of organizing data other than a primary key, or could simply create its own primary key without losing any value from the original data).

I've tried to create my own primary key and tell Django that I want to serialize it. But Django always overrides me and uses serialize=False

I am not entirely sure why Django does that, but at least that's one decision out of my hands.

PS: I can think of one scenario where I would've liked to serialize my primary key. If it was a foreign key in a different table. But it's really a hypothetical, I have not actually come across that use case practically.

Dr Phil
  • 430
  • 5
  • 17
  • I really still don't understand when this is used. It seems it might be called if you call something like `json.dumps` on your data? Any other time? – Akaisteph7 Aug 14 '23 at 14:28