Showing content from https://github.com/wq/django-rest-pandas/issues/29 below:
models with ID columns not named ID cause exception · Issue #29 · wq/django-rest-pandas · GitHub
With a model that has an ID column not named ID, and the following code:
class TestView(PandasView):
queryset = WeirdIDModel.objects.all()
serializer_class = WeirdIDModelSerializer
when trying to query the class, you get an error KeyError, "id".
Reading that stack trace and the source I see that this is because by default we run "set_index('id') on the df, and this can be fixed in three obvious ways:
- set pandas_index on the meta of the serializer
- override get_index_fields on the serializer
- override get_dataframe
- setting the index seems to be needless duplication (I've already written once what the index of this model is)
- an override to get_index_fields I think would just create behaviour that should be default:
WeirdIDModelSerializer._meta.pk.name gives the ID, for my version of Django at least
so something like
try:
id_name = (self.<field for model>)._meta.pk.name
except:
id_name = 'id'
default_fields = [id_name]
- Thinking about it more, and reading the source, it seems odd to even set an index, especially without preserving the original column - I think that DRF classes that we're mimicking would by default pass the index through to the client, and since I'm trying to drop-in replace some existing code which might rely on that, I'll have to replicate that manually.
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4