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

Error: Object of type datetime is not JSON serializable #146

Closed
matthew-a-dunlap opened this issue Mar 7, 2022 · 6 comments
Closed

Error: Object of type datetime is not JSON serializable #146

matthew-a-dunlap opened this issue Mar 7, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@matthew-a-dunlap
Copy link

Hello!

This project is great. I've run into an issue though. I was trying to run this (unfinished) query:

select m.id, m.pub_name, m.pub_id, m.contents_restricted, 
	array_agg(s.updated_at) as sub_dates
from main_manuscript m
left join main_submission s on m.id=s.manuscript_id
group by m.id, m.pub_name, m.pub_id, m.contents_restricted
order by m.id

The problem is that django-sql-dashboard errors out because it doesn't know how to serialize the s.updated_at DateTime field for working with array_agg.

As far as I could tell the only way to fix this would be to fork the codebase? Am I missing something? Thanks!

@matthew-a-dunlap matthew-a-dunlap changed the title Object of type datetime is not JSON serializable Error: Object of type datetime is not JSON serializable Mar 7, 2022
@simonw simonw added the bug Something isn't working label Mar 7, 2022
@simonw
Copy link
Owner

simonw commented Mar 7, 2022

Definitely a bug!

@simonw
Copy link
Owner

simonw commented Mar 8, 2022

Looks like this replicates the bug:

select ARRAY[now()]

@simonw
Copy link
Owner

simonw commented Mar 8, 2022

Or this:

select TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'

This works and returns a column called timestamptz containing 2004-10-19 08:23:54+00:00.

This does not work:

select ARRAY[TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02']

@simonw
Copy link
Owner

simonw commented Mar 8, 2022

This is the code causing the error:

for cell in row:
if isinstance(cell, (dict, list)):
cell = json.dumps(cell)
fixed_row.append(cell)

@simonw simonw closed this as completed in 3f96cf7 Mar 8, 2022
simonw added a commit that referenced this issue Mar 8, 2022
@simonw
Copy link
Owner

simonw commented Mar 8, 2022

@matthew-a-dunlap
Copy link
Author

Awesome! Thanks so much :)

simonw added a commit to simonw/simonwillisonblog that referenced this issue Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants