#33974: Serializer incorrectly serializes Integer ArrayField as string
-----------------------------------------+------------------------
Reporter: arthanson | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 4.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
If you define a model with an ArrayField of type Integer
(PositiveSmallInteger, etc..) and use the built in serializer to json it
will be serialized as an array of strings. Expected is that it will be
serialized as an array of integers. For example, given the model below:
{{{
from django.db import models
# Create your models here.
from django.contrib.postgres.fields import ArrayField
from django.db import models
class Board(models.Model):
pieces = ArrayField(
base_field=models.PositiveSmallIntegerField()
)
}}}
If you create an object with "pieces = [1, 3, 5, ]" and either create a
fixture or call serialize directly you will get an array of strings. The
following management command shows the issue:
{{{
from django.core.management.base import BaseCommand, CommandError
from serializertest.models import Board
from django.core.serializers import serialize
import json
class Command(BaseCommand):
help = ''
def handle(self, *args, **options):
obj = Board.objects.first()
if not obj:
pieces = [1, 3, 5]
obj = Board()
obj.pieces = pieces
obj.save()
print(serialize('json', [obj]))
print(json.dumps(obj.pieces))
}}}
Output will be:
[{"model": "serializertest.board", "pk": 1, "fields": {"pieces": "[\"1\",
\"3\", \"5\"]"}}]
[1, 3, 5]
Note: the serialize output an array of strings, while calling json
directly will give the expected array of integers.
--
Ticket URL: <https://code.djangoproject.com/ticket/33974>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070182faf915a2-bdaa7575-44fd-448e-98ef-77dc77986a78-000000%40eu-central-1.amazonses.com.