Omar Jandali
Omar Jandali

Reputation: 824

Grabbing data from post request and creating new object model error: 'dict' object has no attribute 'data'

I have a django rest framework project. I want to take the standard create method for Model View Sets in django rest framework. I want to create a new model object based on the data passed in but I also wanted to override some of the fields if they are passed in through url arguments.

So if there are no url arguments => just create a default object based soley on the post request. if there is a namespace arguments => create default object based on post request but use the namespace url argument. if there is a namespace and path arguments => create default object based on post request but use the namespace and path url arguments.:

I am getting the following error:

AttributeError at /api/v2/preferences/namespace1/
'dict' object has no attribute 'data'
Request Method: POST
Request URL:    http://127.0.0.1:8000/api/v2/preferences/namespace1/
Django Version: 2.2.1
Exception Type: AttributeError
Exception Value:    
'dict' object has no attribute 'data'
Exception Location: C:\Users\jandali\AppData\Local\Programs\Python\Python37-32\lib\site-packages\rest_framework\mixins.py in create, line 19
Python Executable:  C:\Users\jandali\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version: 3.7.3
Python Path:    
['C:\\Users\\jandali\\desktop\\ozone-backend\\ozone-framework-python-server',
 'C:\\Users\\jandali\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
 'C:\\Users\\jandali\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
 'C:\\Users\\jandali\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
 'C:\\Users\\jandali\\AppData\\Local\\Programs\\Python\\Python37-32',
 'C:\\Users\\jandali\\AppData\\Roaming\\Python\\Python37\\site-packages',
 'C:\\Users\\jandali\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
Server time:    Tue, 9 Jul 2019 21:36:07 +0000

Here is the model view set:

    @permission_classes((IsAuthenticated))
    def create(self, request, *args, **kwargs):
        # print(request)
        namespace = self.kwargs.get('namespace', None)
        path = self.kwargs.get('path', None)
        if namespace is None and path is None:
            return super().create(request)
        if namespace and path is None:
            data = {
                "person":self.request.user,
                'version':self.request.data['version'],
                'namespace':namespace,
                'path':self.request.data['path'],
                'value':self.request.data['value'],
                'user_id':self.request.user.id,
            }
            return super().create(data)
        if namespace and path:
            data = {
                "person":self.request.user,
                'version':self.request.data['version'],
                'namespace':namespace,
                'path':path,
                'value':self.request.data['value'],
                'user_id':self.request.user.id,
            }
            return super().create(data)

Upvotes: 1

Views: 68

Answers (1)

Dharanidhar Reddy
Dharanidhar Reddy

Reputation: 878

I'm sure you are directly or indirectly inheriting CreateModelMixin. You have overridden create method and passing a dictionary to its super. That's where it is failing.

Instead, handle everything on your own in this class or override the perform_create method or define the save method in your serializer. In the last two options, you need a serializer.

The better option would be to handle complete data in the body. You don't have to take things from URL args. That would be more RESTful.

Upvotes: 1

Related Questions