Opened 7 years ago

Last modified 12 months ago

#5711 assigned Bug

serialize doesn't work with extra()

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

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

Download all attachments as: .zip

Change History (16)

Changed 7 years ago by wnielson

comment:1 Changed 7 years ago by wnielson

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

  • Has patch set

Changed 7 years ago by anonymous

comment:3 Changed 7 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 7 years ago by wnielson

comment:4 Changed 7 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

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 6 years ago by bthomas

  • Cc bthomas@… added

comment:6 Changed 5 years ago by orokusaki

(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 Changed 4 years ago by gabrielhurley

  • Severity set to Normal
  • Type set to Bug

comment:8 Changed 4 years ago by kmike

  • Cc kmike84@… added
  • Easy pickings unset

comment:9 Changed 4 years ago by julien

  • Needs tests set

comment:10 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:11 Changed 22 months ago by senko

  • Owner changed from nobody to senko
  • Status changed from new to assigned

comment:12 Changed 22 months 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.

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

comment:13 Changed 12 months ago by timo

  • Patch needs improvement set

Patch no longer merges cleanly.

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