0

I have the following data coming in to a Django view using the Django REST framework -

{'varmst_starttype': 'SCH_DATE', 'varmst_offsets': '0000SUNDAY1', 'owner_name': 'Operations', 'varmst_publish': 'N', 'varmst_calc': 'Y', 'varmst_public': 'Y', 'varmst_desc': None, 'varmst_name': 'var_date ', 'varmst_readonly': 'N', 'varmst_value': 20140911, 'varmst_startdt': datetime.datetime(1899, 12, 30, 0, 0), 'varmst_lstchgtm': datetime.datetime(2014, 9, 10, 22, 0, 28), 'varmst_id': 1867, 'varmst_lstval': None, 'varmst_startcal': 0, 'varmst_type': 3}

What I want to do is use the key value on 'owner_name' to get the 'id' by going -

ownername = request.DATA['owner_name']
ownerid = Owner.objects.filter(owner_name=ownername).values_list('owner_id')[0]

I then want to remove the 'owner_name': 'Operations' and replace it with 'owner_id': 235

When I try to just get a response on ownername I get the following error -

list indices must be integers, not str

This is my view that I'm working off of -

    def put(self, request, format=None):
        data = request.DATA
        data.update({'owner_id': 786})
        return HttpResponse(data)

I've updated to remove the serializer as the issue is before I even get to serialization but with trying to modify the request data. Even on a simple trying to update the request.DATA I get errors. Using the above PUT I get the following -

'list' object has no attribute 'update'

Which makes sense because my understanding is that this is a dict. But according to this -

http://stackoverflow.com/questions/1024847/add-to-a-dictionary-in-python

The same process should work for a dict?

This attempt fails with the original Title of the question.

    def put(self, request, format=None):
        data = request.DATA
        data.['owner_id'] = 786
        return HttpResponse(data)
whoisearth
  • 4,080
  • 13
  • 62
  • 130
  • I don't see where you actually try to make that replace- it seems likely that bit of code is where the mistake is. – Paul Becotte Sep 17 '14 at 02:22
  • That's the thing everything I've tried has not worked. I've tried `data.update({'owner_id': 235})`. I've tried `data['owner_id'] = 235` and other variations all give the same error. Same goes for deleting a value from the dict. – whoisearth Sep 17 '14 at 02:28
  • data isn't the dict... it is the result of request.DATA['owner_name']... – Paul Becotte Sep 17 '14 at 02:40

1 Answers1

1

As I mentioned in the commment-

data is the bit of info from request.DATA['owner_name'], which is a string.

Tring to access data['another string'] will not work.

you want data = request.DATA instead.

EDIT

Sorry- I forgot that request.DATA was immutable. You need to do

data = request.DATA.copy()
Paul Becotte
  • 9,767
  • 3
  • 34
  • 42