views:

97

answers:

3

I'm trying to do a Django database save from a form where I don't have to manually specify the fieldnames (as I do in the 2nd code block), the way I am trying to do this is as below (1st code block) as I got the tip from another S.O. post. However, when I try this I get the error "dictionary update sequence element #0 has length 4; 2 is required", I even tried it, as below, with just a testdict dictionary, instead of the request.POST, but am still getting the error.. obviously the field value is fine since it works in the 2nd code block, so I am stumped as to why this is happening, would appreciate if anyone can shed any light on this for me... thanks

trying it this way gives the error:

testdict = {'name':'account_username','value':'vvvvvv'}
for name, value in testdict.iteritems():
    if name != '' and name != 'top_select':
         b = Twitter(**dict((name, value)))
         b.save()
>>> dictionary update sequence element #0 has length 4; 2 is required

but this works fine:

b = Twitter(account_username='vvvvvv')
b.save()
+1  A: 

Not sure what you are trying to do, but maybe you want something like this

b = Twitter(**{name: value})

But to get the equivalent to Twitter(account_username='vvvvvv') you would need something like this

Twitter(**{testdict['name'], testdict['value']})

where testdict would only contain a single entity to send to Twitter()

Then the code would look more like this

test_twits = [{'name':'account_username','value':'vvvvvv'},
              {'name':'account_username','value':'wwwwww'},
              ]
for twit in test_twits:
    name = twit['name']
    value = twit['value']
    if name != '' and name != 'top_select':
         b = Twitter(**{name: value})
         b.save()
gnibbler
Why not just `b = Twitter(**{name: value})`?
Jack Kelly
will try both ways, its for a django DB call, so not sure exactly what it needs as the normal way they suggest is the 2nd code block but I don't want to do it that way since I want to iterate through without having to manually type the name
Rick
@Jack, probably better I think. I changed it
gnibbler
A: 

Correct me if I am wrong.

From your second code snippet I take it that the Twitter class needs account_username as a keyword argument. When you are iterating through the dictionary using iteritems you are passing the name - i.e. the key of the dictionary as the keyword argument to the class. Isn't this wrong? The dictionary's keys are name and value, _not _ account_username. I believe you need the one of values from the dictionary to be passed as keyword argument, not one of the keys.

Manoj Govindan
yeah, I guess the syntax is different using a regular dictionary as opposed to a request.POST from django, so I was doing it wrong
Rick
A: 

just do this:

dict(((name, value),))

'dict' takes a sequence of key, value tuples whereas you are giving it one key, value tuple.

The reason it says '... sequence element #0 has length 4' is because the key 'name' from testdict has a length of 4.

möter