ForestAdmin / django-forestadmin

🐍 Django agent for Forest Admin to integrate directly to your existing Django backend application.
https://www.forestadmin.com
GNU General Public License v3.0
122 stars 18 forks source link

fix(utils): prevent error in exporting tables with non-"id" primary keys #110

Closed snwessel closed 2 years ago

snwessel commented 2 years ago

Export to CSV Bug

I noticed that when exporting CSVs through the Forest Admin front-end, I was getting the following error:

Traceback (most recent call last):
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/swessel/Desktop/Repos/django-forestadmin/django_forest/resources/utils/resource.py", line 13, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/swessel/Desktop/Repos/django-forestadmin/django_forest/resources/views/csv.py", line 36, in get
    self.fill_csv(data, writer, params)
  File "/Users/swessel/Desktop/Repos/django-forestadmin/django_forest/resources/utils/csv.py", line 27, in fill_csv
    res = self.fill_csv_relationships(res, record, data, params)
  File "/Users/swessel/Desktop/Repos/django-forestadmin/django_forest/resources/utils/csv.py", line 19, in fill_csv_relationships
    res[name] = related_res['attributes'][params[f'fields[{name}]']]
KeyError: 'id'

After a bit of debugging, I noticed that, this csv utility seems to assume that my table has a column named "id", and that foreign keys in my table are pointing towards primary key columns named "id". However, my database doesn't follow those naming conventions.

vamonte commented 2 years ago

Thank's for your work, I'm going to check it this week.

vamonte commented 2 years ago

@snwessel seems fixed in this PR https://github.com/ForestAdmin/django-forestadmin/pull/112/files