views:

51

answers:

1

I can't seem to figure out a good way to do this,

I have a bunch of form input fields and rather than having to do like below where I have to type out each fieldname = "" and such, is there a way to code this so it would automatically save each field from within the form?

b = Modelname(fieldname=request.POST['fieldname'])
b.save()

I realize security issues of this but could be negated by running it through a list that checks for a valid form value.

+3  A: 

Sure, request.POST.items() and request.POST.iteritems() work just like the methods of the same name in a dict, returning resp. a list and an iterator of all (name, value) pairs (2-items tuple) in that dict-like object. If there are multiple values for a name, that only gives you the last one; if you want all of them, use request.POST.iterlists(), which in the value slot of each pair has a list of all the values for fields with that name.

So, assuming for example that you don't care about duplicates or know there are none, even a snippet as short as:

for name, value in request.POST.iteritems():
    ModelName(**dict( [ (name, value) ] )).save()

might suffice, though it might definitely be better to add some validation checking of the name/value pair before saving it this way;-).

Alex Martelli
thanks, I was fumbling around with this for a while, I knew there must be some elegant way to do it but couldn't come up with what it was.. thanks, will give it a try
Rick
I'm trying this but am getting an error:"dictionary update sequence element #0 has length 16; 2 is required" not sure what this is referring to, I replaced ModelName with my model's name.... I have just 2 fields that I am inputting and their values both have well more than 2 characters in them (just basic varchar fields) so I'm kind of confused by this
Rick
EDIT: misread the error message before, should have said that my field's value is less than 16, (its like 6 characters) where it seems to be saying its 16) -- also, just to clarify, I a tried returning the first value and name from that loop (to check the output) and both came out correctly, as the input field's and its value, but am still getting this error message
Rick
@Rick, looks like you're **not** using the specific syntax I gave, `**dict((name, value))` -- that tuple I'm passing to `dict` definitely **does** have length two, so you must be using different code (which ends up passing something else than a 2-items tuple). Just guessing: maybe you're **not** using double parentheses, as I am and you absolutely also must? Could be some other syntax blooper on your part, but this one seems likeliest.
Alex Martelli
@Alex, thanks for the response, I copied your post exactly and just changed the model name, at least I am pretty sure, will give it a look over and try to figure what it is, thanks
Rick
yeah, its still doing this even why I try with just a custom dict that I make, well its a separate issue from this so I'll start a new post, thanks for the tip on how to do this as I'm sure its right, just something is causing this other issue
Rick
Aha, my bad -- I missed one level of parentheses, editing to add. BTW, in your other question, you're not using the same kind of `iteritems` as you'd be getting from `request.POST.iteritems()`, as responders to that Q have already spotted.
Alex Martelli
that solved it.. thanks for your help in this, I really appreciate it as this is a big time saver in the longrun for me
Rick