Hello,
I'm new to Django and having a problem with serialization.
I am trying to serialize a model that has a CharField with choices,
and the json serializer returns json with the choices' database values
instead of display values.
Is there a way to get the json serializer to use the choices's display
values?
Here is a brief example (I haven't tested it but I think it
illustrates the problem):
In the models file:
from django.db import models
from django.utils.translation import ugettext_lazy as _
class Foo(models.Model):
SAMPLE_CHOICES = (('db_name1', _('Display Name 1')),
('db_name_2',_('Display Name 2')))
choice_field = models.CharField(max_length = 10, choices =
SAMPLE_CHOICES)
If there is one Foo in the database, with a value of db_name1 for
choice_field, and I call get_foo_json(), with:
def get_foo_json()
foos = Foo.objects.all()
json_serializer = serializers.get_serializer("json")()
response = HttpResponse()
json_serializer.serialize(foos, ensure_ascii=False,
stream=response)
return response
The response is something like:
'Content-Type: text/html; charset=utf-8\n\n[{"pk": 1, "model":
"MyApp.foo", "fields": {"choice_field": "db_name1"}}]'
I would like to get something like:
'Content-Type: text/html; charset=utf-8\n\n[{"pk": 1, "model":
"MyApp.foo", "fields": {"choice_field": "Display Name 1"}}]'
Thank you for your help,
-Nate
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.