Comments (5)
This use case wasn't fully supported before, but it was an easy fix. I added some more descriptive error messages and also updated the documentation here. The error is that if you have a rest_pandas
renderer in DEFAULT_RENDERER_CLASSES
, you need to import rest_pandas
before importing anything from rest_framework.views
or you may end up with a recursive import.
One way to fix the import order is to add rest_pandas
to your INSTALLED_APPS
, though you'll also want to import PandasMixin
to add to your own views.
from django-rest-pandas.
Response data is a OrderedDict, not a DataFrame! Did you extend PandasMixin?
In my case, the above error was directly related to pagination and I didn't want to disable DEFAULT_PAGINATION_CLASS. As a quick workaround for the problem, I override ListMixin and check to see if the URL parameter "format" exists. I don't use a format extension (/path.csv) so I didn't bother to add it.
from django.conf import settings
from rest_framework import mixins
class PaginatedPandaListMixin(mixins.ListModelMixin):
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
# Check if format parameter overridden
try:
format_param = settings.URL_FORMAT_OVERRIDE
except AttributeError:
format_param = 'format'
# Don't paginate if response is for DRP or API
if request.GET.get(format_param) and not request.GET.get(format_param) == 'api':
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
I then use the above mixin on my viewsets and all is well.
class CustomViewSet(PandasMixin, PaginatedPandaListMixin, ModelViewSet):
from django-rest-pandas.
Hey there. Thanks for the response. I've followed your instructions listed above, and am still running into issues. In my settings file, I've included rest_pandas in my included apps. Then in my models, I defined a objects = DataFrameManager(), right before the class Meta definition. My serializer is a ModelSerializer. And my View is passing PandasMixin, ListAPIView. Listed below is my code.
`
(serializer.py)
class FfAllFactorsDailyPandaSerializer(ModelSerializer):
class Meta:
model = FfAllFactorsDailyModel
fields = (
'date',
'mktrf',
'smb',
'hml',
'rf',
'umd',
)
(views.py)
class FfAllSeriesView(PandasMixin, ListAPIView):
queryset = FfAllFactorsDailyModel.objects.all()
serializer_class = FfAllFactorsDailyPandaSerializer
permission_classes = (FfAllPermission,)
def filter_queryset(self, qs):
# At this point, you can filter queryset based on self.request or other
# settings (useful for limiting memory usage). This function can be
# omitted if you are using a filter backend or do not need filtering.
return qs
`
Much to my chagrin, I'm still getting a "Response data is a <class 'rest_framework.utils.serializer_helpers.ReturnList'>, not a DataFrame!" error. In short, what am I doing wrong? I thought the (objects = DataFrameManager()) defined in the model, would return a DataFrame. Thanks in advance for the assist. Take care and have a good day.
Sincerely,
JG
from django-rest-pandas.
Thanks very much for our efforts, @sheppard - but I'm having a similar issue to JG, and I think I'm close. I've installed the latest rest_pandas
from master
. I've added rest_pandas
to my INSTALLED_APPS
. Here's are my settings:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_METADATA_CLASS': 'home.adapters.TypesFiltersMetadata',
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 10,
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
'rest_pandas.renderers.PandasExcelRenderer',
),
}
I've modified my ViewSets
to look like this:
class MyExampleViewSet(PandasMixin, ReadOnlyModelViewSet):
queryset = MyExampleModel.objects.all()
serializer_class = MyExampleSerializer
permission_classes = (MyPermission,)
filter_backends = (DjangoFilterBackend,)
filter_fields = (
'date',
)
I can browse the HTML version of the DRF API, and xlsx
appears as an option in the drop down alongside json
and html
. However, when I select xlsx
I'm getting this error, despite having extended PandasMixin
:
Response data is a OrderedDict, not a DataFrame! Did you extend PandasMixin?
I've started digging through the code, but any help you could provide would be fantastic. Thanks again for your efforts on DRP - much appreciated!
from django-rest-pandas.
Remove DEFAULT_PAGINATION_CLASS in your settings.
from django-rest-pandas.
Related Issues (20)
- models with ID columns not named ID cause exception HOT 3
- Permission_class is ignored HOT 2
- Set filename in view HOT 1
- Use as a method in a ViewSet HOT 4
- license in pypi HOT 1
- Calling `PandasBaseRenderer().render(data)` throws `TypeError` HOT 2
- add Django 2.0 and DRF 3.8 to Travis tests HOT 2
- Response data is a ReturnList, not a DataFrame! Did you extend PandasMixin? HOT 2
- date_format not used when creating csv HOT 2
- how to return a json Response when query_params with 'format=xlsx' HOT 2
- Template does not exist rest_pandas.html HOT 1
- Links to `wq/chart.js` are no longer necessary in the README. HOT 1
- Not clear on how to return nested data
- how to pass id parameter example y = Times.objects.get(id=id) or Profile = Profile.objects.get(user=request.user) because i want individual chart for any user but i don't understand how to pass this parameters on pandas view
- Demo returns Access Denied
- Possible to Access ExcelRenderer from PandasSimpleView?
- Change comma separator from ',' to something else (e.g.: ';')
- make it Python 3.6 + django 2.2+ only
- Pypi release?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-rest-pandas.