Opened 10 years ago

Last modified 11 months ago

#5711 assigned Bug

Allow non-field attributes to be serialized

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


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 10 years ago.
patch2.diff (2.0 KB) - added by anonymous 10 years ago.
serialize_extra_support.diff (2.4 KB) - added by wnielson 10 years ago.

Download all attachments as: .zip

Change History (17)

Changed 10 years ago by wnielson

Attachment: patch.diff added

comment:1 Changed 10 years ago by wnielson

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 Changed 10 years ago by anonymous

Has patch: set

Changed 10 years ago by anonymous

Attachment: patch2.diff added

comment:3 Changed 10 years ago by wnielson

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)

Changed 10 years ago by wnielson

comment:4 Changed 9 years ago by Jacob

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 Changed 8 years ago by Bob Thomas

Cc: bthomas@… added

comment:6 Changed 7 years ago by Oroku Saki

(Moved example from duplicate ticket# 12862 to here)

Let's say I have this class:

class Cart(models.Model):
    account = models.ForeignKey(Account)
    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 Changed 6 years ago by Gabriel Hurley

Severity: Normal
Type: Bug

comment:8 Changed 6 years ago by Mikhail Korobov

Cc: kmike84@… added
Easy pickings: unset

comment:9 Changed 6 years ago by Julien Phalip

Needs tests: set

comment:10 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:11 Changed 4 years ago by senko

Owner: changed from nobody to senko
Status: newassigned

comment:12 Changed 4 years ago by senko

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.


comment:13 Changed 3 years ago by Tim Graham

Patch needs improvement: set

Patch no longer merges cleanly.

comment:14 Changed 11 months ago by Simon Charette

Summary: serialize doesn't work with extra()Allow non-field attributes to be serialized
Note: See TracTickets for help on using tickets.
Back to Top