{{{
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.
* Attachment "models.py" added.
example model file with arrayfield
* Attachment "test_serializer.py" added.
management command that shows the issue
* status: new => closed
* resolution: => wontfix
* component: Uncategorized => Core (Serialization)
* type: Uncategorized => New feature
Comment:
Thanks for the report however (de-)serialization works for me with
`ArrayField(base_field=models.PositiveSmallIntegerField())`. Are there any
practical issues with the current approach (except for your preferences)?
Other complex fields (such as `JSONField`) are also serialized as strings.
I'm afraid that the proposed change is backward incompatible and you can
always use a custom serializer, if you need a different format.
--
Ticket URL: <https://code.djangoproject.com/ticket/33974#comment:1>
Comment (by Claude Paroz):
FWIW, I would be favorable to fix this, if not too complex to do.
--
Ticket URL: <https://code.djangoproject.com/ticket/33974#comment:2>
Comment (by Mariusz Felisiak):
Replying to [comment:2 Claude Paroz]:
> FWIW, I would be favorable to fix this, if not too complex to do.
PoC is always welcome, I'm afraid that this will brake existing fixtures.
--
Ticket URL: <https://code.djangoproject.com/ticket/33974#comment:3>
Comment (by Claude Paroz):
Arthur, would you like to work on a possible fix? (with no guarantee it
will be accepted eventually)
--
Ticket URL: <https://code.djangoproject.com/ticket/33974#comment:4>