Skip to content

Binary parameter handling for GeoDjango #2169

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

eduzen
Copy link

@eduzen eduzen commented Jul 27, 2025

Description

hey folks, this is my very first PR in the django-commons. I did this because I faced the issue myself in one project that I'm working on. I don't know how I can test this in that project. So I will wait for some of you to tell me how I can do that... but in the meantime this is the fix for the SQL Explain functionality for GeoDjango queries with binary parameters.

Fixes #423

SQL Explain feature throws a 500 error when used with GeoDjango queries containing binary parameters. This affects any query using PostGIS functions like ST_GeomFromEWKB(), ST_Distance_Sphere(), etc.
I'm trying to create a robust binary parameter handling system:

  1. Enhanced Parameter Encoding (tracking.py)
  • Detects binary data (bytes, bytearray) during parameter logging
  • Encodes binary data as base64 with a special marker: {"djdt_binary": "base64_data"}
  • Preserves exact binary integrity through JSON serialization
  1. Parameter Reconstruction (forms.py)
  • Added _reconstruct_params() function to decode stored parameters
  • Recursively handles nested structures (lists, dicts) containing binary data
  • Restores original binary data from base64 encoding
  1. Updated SQL Operations
  • Modified select(), explain(), and profile() methods to use parameter reconstruction
  • Added helper method _get_query_params() to eliminate code duplication

Checklist:

  • I have added the relevant tests for this change.
  • I have added an item to the Pending section of docs/changes.rst.

def _get_query_params(self):
"""Get reconstructed parameters for the current query"""
query = self.cleaned_data["query"]
return _reconstruct_params(json.loads(query["params"]))
Copy link
Member

Choose a reason for hiding this comment

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

Do you think this would be improved by making use of the json.loads(..., cls) argument?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error debugging GeoDjango queries
2 participants