Code

Opened 5 years ago

Closed 4 years ago

#11244 closed (wontfix)

serialization only works for iterable objects

Reported by: epper Owned by: nobody
Component: Core (Serialization) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This works:

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

This doesn't and throws TypeError:'SomeModel' object is not iterable:

from django.core import serializers
data = serializers.serialize("json", SomeModel.objects.get(pk=1))

I think that should be possible to serialize single objects too.

(Es: I often need to serialize a object, or better... some fields of a single object when implementing ajax views)

Attachments (0)

Change History (4)

comment:1 follow-up: Changed 5 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

It's trivially easy do this in your calling code:

from django.core import serializers
data = serializers.serialize("json", [SomeModel.objects.get(pk=1)])

I don't see why the code/doc for serializers.serialize should be made more complicated just to handle this.

comment:2 in reply to: ↑ 1 Changed 5 years ago by epper

Replying to kmtracey:

It's trivially easy do this in your calling code:

from django.core import serializers
data = serializers.serialize("json", [SomeModel.objects.get(pk=1)])

I don't see why the code/doc for serializers.serialize should be made more complicated just to handle this.

Because the resulting json string does not represent an object but an array of one single object and I don't see why the code/doc of the client side javascript should be made more complicated just to handle an array when expecting an object ;)

comment:3 Changed 4 years ago by orokusaki

  • Resolution wontfix deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Unreviewed to Design decision needed

@kmtracy I might be overstepping my bounds (ie, feel free to slap me in the face and re-close this ticket if I am), but I strongly agree with epper. It's easy to use one object in a list but then you have to either iterate through the object (with only one iteration) or use your JavaScript to pull out the one object like my_object[0] over and over again (DRY comes to mind). I would suggest that if an object (instead of a QuerySet) is passed into the method that the output reflect this as well. It just seems right. When it comes to an API (JSON in my case) it's all about clean code and clean output.

comment:4 Changed 4 years ago by kmtracey

  • Resolution set to wontfix
  • Status changed from reopened to closed

Please don't reopen wontfixed issues without first gaining some consensus for it on the development list. See: http://docs.djangoproject.com/en/dev/internals/contributing/#id1.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.