2

Here is some code from a Django 3.1 migration:

        migrations.AlterField(
            model_name='foo',
            name='blarg',
            field=models.BigIntegerField(default=theapp.util.make_id, primary_key=True,
                      serialize=False),
        ),

What does the serialize=False mean in this context? I read some code and docs, and it wasn't obvious.

dfrankow
  • 20,191
  • 41
  • 152
  • 214
  • @Akaisteph7 I agree that the question you propose is a dup, so I'm going to say "yes". However, that question also does not have an answer that answers my question. I see it's related to Django serializers, but it's not obvious to me where those serializers are used. – dfrankow Aug 12 '23 at 16:37
  • Yes, I don't think it's completely clear yet as Django has no docs on this for some reason. The answer in this post has actually been the most helpful to me. But it's good to mark a duplicate either ways. – Akaisteph7 Aug 14 '23 at 14:30

1 Answers1

2

This means the field will not be part of the serialized object.

for example:

from django.db import models

# You hava a model
class MyModel(models.Model):
    myfield = models.TextField(serialize=False)

# dump data
from django.core import serializers   
data = serializers.serialize("json", MyModel.objects.all())

# myfield will not exist in data
print(data) 

I guess in your context, the field is some automatic generated field.

you can reference this post

Allen Shaw
  • 1,164
  • 7
  • 23
  • Why does Django decide to specify it, and why isn't it documented at all? Is it ok to remove it? When should and shouldn't I use it or leave it? – odigity May 19 '22 at 23:19
  • So this will only apply when you call `django.core.serializers.serialize` on it or are there other actions that trigger this flag? It seems this might get triggered on `json.dumps` calls as well, anything else? – Akaisteph7 Aug 11 '23 at 21:59