Opened 17 years ago

Closed 2 years ago

#5711 closed Bug (wontfix)

Allow non-field attributes to be serialized

Reported by: valankar@… Owned by: Dan F
Component: Core (Serialization) Version: dev
Severity: Normal Keywords:
Cc: bthomas@…, kmike84@…, David Wobrock Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

It seems if I create a request set with extra(), the fields are not in the output generated by serialize().

Attachments (3)

patch.diff (1.9 KB ) - added by wnielson 17 years ago.
patch2.diff (2.0 KB ) - added by anonymous 17 years ago.
serialize_extra_support.diff (2.4 KB ) - added by wnielson 17 years ago.

Download all attachments as: .zip

Change History (22)

by wnielson, 17 years ago

Attachment: patch.diff added

comment:1 by wnielson, 17 years ago

I added a quick patch to allow extra attributes to be serialized. Here's an example:

serialize(Blog.objects.all(), fields=('title',), attrs=('entry_count',))

where 'entry_count' is an attribute (not a Django field) of Blog.

comment:2 by anonymous, 17 years ago

Has patch: set

by anonymous, 17 years ago

Attachment: patch2.diff added

comment:3 by wnielson, 17 years ago

I added another patch which I think is better because it works directly with extra. If a QuerySet passed to serialize contains something in _select and the kwargs extra=True is passed, then those fields will be included in the output. Example:

serialize(Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"}), extra=True)

by wnielson, 17 years ago

comment:4 by Jacob, 17 years ago

Triage Stage: UnreviewedAccepted

In general the serializer framework needs ways of adding arbitrary extra fields onto serialized data. This patch is a bit too specialized, but I'm leaving it open so we can keep an eye on this use case.

comment:5 by Bob Thomas, 16 years ago

Cc: bthomas@… added

comment:6 by Oroku Saki, 15 years ago

(Moved example from duplicate ticket# 12862 to here)

Let's say I have this class:

class Cart(models.Model):
    account = models.ForeignKey(Account)
    
    @property
    def total(self):
        total = 0
        for item in self.item_set:
            total += item.price
        return total

Obviously if I were to serialize this model, I would want the total to be serialized. I wouldn't mind having to include it in fields=() argument of serialize().

comment:7 by Gabriel Hurley, 14 years ago

Severity: Normal
Type: Bug

comment:8 by Mikhail Korobov, 14 years ago

Cc: kmike84@… added
Easy pickings: unset

comment:9 by Julien Phalip, 14 years ago

Needs tests: set

comment:10 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:11 by senko, 12 years ago

Owner: changed from nobody to senko
Status: newassigned

comment:12 by senko, 11 years ago

Needs tests: unset

Here's a pull request adding an extra keyword argument to serialize() method that allows the user to specify non-field attributes on the model that they wish to serialize. Also added a note in the docs that deserialization of models serialized with extra won't work as deserializer wants all the fields to actually be model fields.

PR: https://github.com/django/django/pull/1176

comment:13 by Tim Graham, 11 years ago

Patch needs improvement: set

Patch no longer merges cleanly.

comment:14 by Simon Charette, 8 years ago

Summary: serialize doesn't work with extra()Allow non-field attributes to be serialized

comment:15 by Dan F, 2 years ago

Not sure if we still want this, but I filed an updated PR at https://github.com/django/django/pull/16004, just to try out contributing.

comment:16 by David Wobrock, 2 years ago

Cc: David Wobrock added

comment:17 by David Wobrock, 2 years ago

Needs tests: set
Owner: changed from senko to Dan F

comment:18 by Dan F, 2 years ago

I would prefer to close this over submitting code, unless someone who is still around is actually requesting this feature.

comment:19 by Dan F, 2 years ago

Resolution: wontfix
Status: assignedclosed

Given what I see (last changes were 13 years ago), I am closing this.

I'm fine if someone wants to re-open it who actually wants the feature. Then we can talk about what they want, to help with the discussion on https://github.com/django/django/pull/16004.

Note: See TracTickets for help on using tickets.
Back to Top