Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds RawQuerySet to serializer #37

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion djgeojson/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from django.db.models.base import Model
from django.db.models.query import QuerySet, ValuesQuerySet
from django.db.models.query import RawQuerySet
from django.forms.models import model_to_dict
from django.core.serializers.python import (_get_model,
Serializer as PythonSerializer,
Expand Down Expand Up @@ -122,7 +123,7 @@ def end_object(self, obj):
self.handle_field(obj, field)

# Add extra-info for deserializing
if hasattr(obj, '_meta'):
if hasattr(obj, '_meta') and self.deserializing_extra:
self._current['properties']['model'] = smart_text(obj._meta)

# If geometry not in model fields, may be a dynamic attribute
Expand All @@ -148,6 +149,7 @@ def end_serialization(self):
self.options.pop('simplify', None)
self.options.pop('bbox', None)
self.options.pop('bbox_auto', None)
self.options.pop('deserializing_extra', None)

# Optional float precision control
precision = self.options.pop('precision', None)
Expand Down Expand Up @@ -354,6 +356,7 @@ def serialize(self, queryset, **options):
self.bbox_auto = options.get("bbox_auto", None)
self.srid = options.get("srid", GEOJSON_DEFAULT_SRID)
self.crs = options.get("crs", True)
self.deserializing_extra = options.get("deserializing_extra", True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see where do you use this ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a way to remove useless information in generated json in some cases (model property).

json = GeoJSONSerializer().serialize(raw_query_set, deserializing_extra= False, geometry_field='the_geom')


self.start_serialization()

Expand All @@ -366,6 +369,12 @@ def serialize(self, queryset, **options):
elif isinstance(queryset, QuerySet):
self.serialize_queryset(queryset)

# a geometry field, "geom" could be retrieve with AsText(geom)
# for example
elif isinstance(queryset, RawQuerySet) and \
self.properties is not None :
self.serialize_queryset(queryset)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you test self.properties ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's empirical: without "properties" option, the serialization doesn't work.


self.end_serialization()
return self.getvalue()

Expand Down