tolomea / django-data-browser

Django app for user friendly querying of Django models
BSD 3-Clause "New" or "Revised" License
350 stars 28 forks source link

Support HashId type for Id colum #32

Closed mands closed 3 years ago

mands commented 3 years ago

Hi, great product, really useful! Am getting the following error when trying to load id's for some of our objects that use HashIds (https://github.com/nshafer/django-hashid-field),

[17:23:45] [ERROR] Internal Server Error: /site/data-browser/query/dp_core.Report/name,published,style_header,width,source_url,id.json
Traceback (most recent call last):
  File "/home/m/Dev/projects/dp/dp-server/.venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/m/Dev/projects/dp/dp-server/.venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib64/python3.9/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/m/Dev/projects/dp/dp-server/.venv/lib/python3.9/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/m/Dev/projects/dp/dp-server/.venv/lib/python3.9/site-packages/data_browser/views.py", line 246, in query
    return _data_response(request, query, media, privileged=True)
  File "/home/m/Dev/projects/dp/dp-server/.venv/lib/python3.9/site-packages/data_browser/views.py", line 292, in _data_response
    results = get_results(request, bound_query, orm_models, True)
  File "/home/m/Dev/projects/dp/dp-server/.venv/lib/python3.9/site-packages/data_browser/orm_results.py", line 274, in get_results
    row_data = _format_table(
  File "/home/m/Dev/projects/dp/dp-server/.venv/lib/python3.9/site-packages/data_browser/orm_results.py", line 162, in _format_table
    return eval("\n".join(code), namespace)
  File "<string>", line 1, in <module>
  File "<string>", line 7, in <listcomp>
  File "/home/m/Dev/projects/dp/dp-server/.venv/lib/python3.9/site-packages/data_browser/types.py", line 143, in <lambda>
    return lambda value: None if value is None else float(value)
TypeError: float() argument must be a string or a number, not 'Hashid'

Looking at the code, I think it's assumed that Id is of NumberType - this is the case in the database, however the ORM has already converted it to a HashId object before django-data-browser gets the value, hence the issue I think.

tolomea commented 3 years ago

It's not as simple as assuming ID's are NumberType, the biggest deployment I know of is almost entirely UUID fields for PK's. At a quick glance I believe it's because HashidField inherits off IntegerField. I will have a poke and see what I can do about this.

tolomea commented 3 years ago

I've made a 4.0.7 release that will hopefully fix your problem. Please try it out and let me know how it goes.

mands commented 3 years ago

Thanks so much for the quick feedback and fix - can confirm it works perfectly!