Opened 17 years ago
Closed 2 years ago
#5711 closed Bug (wontfix)
Allow non-field attributes to be serialized
Reported by: | 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)
Change History (22)
by , 17 years ago
Attachment: | patch.diff added |
---|
comment:1 by , 17 years ago
comment:2 by , 17 years ago
Has patch: | set |
---|
by , 17 years ago
Attachment: | patch2.diff added |
---|
comment:3 by , 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 , 17 years ago
Attachment: | serialize_extra_support.diff added |
---|
comment:4 by , 17 years ago
Triage Stage: | Unreviewed → 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 by , 16 years ago
Cc: | added |
---|
comment:6 by , 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 , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:8 by , 14 years ago
Cc: | added |
---|---|
Easy pickings: | unset |
comment:9 by , 14 years ago
Needs tests: | set |
---|
comment:11 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:12 by , 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.
comment:14 by , 8 years ago
Summary: | serialize doesn't work with extra() → Allow non-field attributes to be serialized |
---|
comment:15 by , 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 , 2 years ago
Cc: | added |
---|
comment:17 by , 2 years ago
Needs tests: | set |
---|---|
Owner: | changed from | to
comment:18 by , 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 , 2 years ago
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
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.
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.