Django

Code

Ticket #2930 (closed: wontfix)

Opened 2 years ago

Last modified 1 year ago

Single objects are not serialized as JSON strings

Reported by: stefan.freyr@gmail.com Assigned to: adrian
Milestone: Component: Core framework
Version: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

It's a bit unintuitive in my opinion that the JSON serializer only handles lists of objects.

For example, the follwing two liner will result in a TypeError ("iteration over non-sequence):

    my_object = MyModel.objects.all()[0]
    data = serializers.serialize("json", my_object)

While changing the first line to:

    my_object = MyModel.objects.all()[:1]

Results in a runnable program.

Shouldn't the JSON serializer be able to serialize single instances?

Attachments

Change History

10/18/06 22:11:50 changed by ubernostrum

The problem, I think, is consistency of interface. The docs state that the serializer expects "any iterator that yields Django objects, but it'll almost always be a QuerySet?". It's not hard to meet that requirement, and it ensures that the serialization interface doesn't have to do all sorts of complicated magic to figure out what you've passed in to it.

01/17/07 16:16:57 changed by anonymous

  • stage changed from Unreviewed to Design decision needed.

01/30/07 16:38:55 changed by jacob

  • status changed from new to closed.
  • resolution set to wontfix.

For consistency, the serializers only operate on lists. Doing it otherwise leads to annoying client code that has to check for code/instance every time. Just use serializers.serialize("json", [my_object]).


Add/Change #2930 (Single objects are not serialized as JSON strings)




Change Properties
Action