0

I'm trying to pass parameters in a Class Based View instance, but I can't figure out the proper way to do that.

My api service works good in the REST Framework view and receive two mandatory parameters (user and language):

enter image description here

enter image description here

I found similar answers but send parameters in return, that is not my situation. This is my call,

_customdictionary = CustomDictionaryViewSet()
_customdictionary.custom_dictionary_kpi(request) 

I've tried and fail with:

_customdictionary.custom_dictionary_kpi(request)
_customdictionary.custom_dictionary_kpi({'language': 1, 'user': 1})
_customdictionary.custom_dictionary_kpi(1,1)
# In all cases i receive status = 500

When I see my error.log, in this part:

class CustomDictionaryViewSet(viewsets.ModelViewSet):
...
    def custom_dictionary_kpi(self, request, *args, **kwargs):
        try:
            import pdb;pdb.set_trace()

Sending request, it shows me:

AttributeError: 'WSGIRequest' object has no attribute 'data'

Sending the dict, it shows me:

AttributeError: 'dict' object has no attribute 'data'

Sending just the values:

AttributeError: 'int' object has no attribute 'data'

api/urls.py

urlpatterns = [
url(r'^api/customdictionary/custom_dictionary_kpi/user/<int:user_id>/language/<int:language_id>', CustomDictionaryViewSet.as_view({'post': 'custom_dictionary_kpi'}), name='custom_dictionary_kpi')
]

api/api.py

class CustomDictionaryViewSet(viewsets.ModelViewSet):
    queryset = CustomDictionary.objects.filter(
        is_active=True,
        is_deleted=False
    ).order_by('id')
    permission_classes = [
        permissions.AllowAny
    ]
    pagination_class = StandardResultsSetPagination

    def __init__(self,*args, **kwargs):
        self.response_data = {'error': [], 'data': {}}
        self.code = 0

    def get_serializer_class(self):
        if self.action == 'custom_dictionary_kpi':
            return CustomDictionaryKpiSerializer
        return CustomDictionarySerializer

    @action(methods=['post'], detail=False)
    def custom_dictionary_kpi(self, request, *args, **kwargs):
        try:
            '''some logic'''
        except Exception as e:
            '''some exception'''
        return Response(self.response_data,status=self.code)

serializers.py

class CustomDictionarySerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomDictionary
        fields = ('__all__')

class CustomDictionaryKpiSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomDictionary
        fields = ('user','language')

web/views.py

class CustomDictionaryView(View):
    """docstring for CustomDictionaryView"""
    def __init__(self,*args, **kwargs):
        self.response_data = {'error': [], 'data': {}}
        self.code = 0

    def get(self, request, *args, **kwargs):
        try:
            _customdictionary = CustomDictionaryViewSet()
            import pdb;pdb.set_trace()
            _customdictionary.custom_dictionary_kpi()   # Here is the call, 
            self.response_data['data'] = _customdictionary.response_data['data']
            self.code = _customdictionary.code

        except Exception as e:
            '''some exception'''

Extra: How could I send an extra optional parameter?

Thanks a lot, any help will be appreciated :)

1 Answer 1

1

Yes that is right data you must be calling the request.data in your custom_dictionary_kpi and you are not giving a parameter as webrequest.

You cam anytime send optional data in the request itself from browser or postman or client like {"language": 1, "user": 1,"foo": "bar"}

and do request.POST.get('foo') on server side.

If you wish to pass data in the class you can do it with keyword argument like this

_customdictionary = CustomDictionaryViewSet()
_customdictionary.custom_dictionary_kpi(request,language=1,user=1) 

and in your method implementation you can access in args e.g. (request) here as a tuple and as a dictionary if you look into kwargs as a dictionary e.g. {"language": 1, "user": 1} here

Try printing or debugging args and kwargs and see.

Sign up to request clarification or add additional context in comments.

2 Comments

Yugandhar Chaudhari when I test my html, changing in custom_dictionary_kpi request.data['user'] to request['user'] it works good, but with that way, if I test my api in the DRF view, i receive: *** TypeError: 'Request' object is not subscriptable. Is there a way to keep working in both scenaries (DRF view and by requesting in html template)
` *** TypeError: 'Request' object is not subscriptable` it means you are trying to treat a dictionary when it is not a dictionary by doing this request['user'] it is a WSGIRequest instance not a dictionary. cleanest way to send is in request data and accessing it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.