Opened 7 years ago

Closed 7 years ago

#8549 closed (duplicate)

Fields added via the extra() QuerySet modifier are not outputted when QuerySet is run through serializer

Reported by: Jim Dalton <jim.dalton@…> Owned by: nobody
Component: Core (Serialization) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

It appears that when you add a field via the extra() method on a QuerySet, that field is not made available to you when you run the QuerySet through the serializer. I am running django version 1.0-beta_1-SVN-8539.

The context in which I discovered this bug, brief:

I set a custom field using extra() so that i could order results by a distance form a given longitude/latitude pair. The custom field uses MySQL numeric functions. Example looks like:

entries = Entry.objects.extra(select={'distance': "SQRT(POW((locations.lat-%s),2) + POW((locations.lon-%s),2))"}, select_params=(str(centerLat), str(centerLng)))
entries = entries.extra(order_by = ['distance'])[:5]

This works. entries[0].distance returns a float correctly, for example.

The bug arises when this QuerySet is serialized and output, as follows:

from django.core import serializers

json_serializer = serializers.get_serializer("json")()
json_serializer.serialize(entries, ensure_ascii=False, stream=response)

The response stream does NOT include the distance attribute, though all other attributes of the model are set. Even explicitly setting the distance attribute in the fields argument of serialize() does not include the distance attribute. I tried changing the serializer to xml to see if it was an artifact of json, but still the attribute was not included. I did confirm that the attribute exists on the QuerySet itself

I tried searching on this bug and I posted this on django-users and found nothing. My apologies if this is not a bug or its been reported elsewhere. I hope this is useful.

Change History (1)

comment:1 Changed 7 years ago by russellm

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

Duplicate of #5711

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