Hi all,

Thanks for every ones help on this. I have a fix now which is working
well.

from django.forms.models import model_to_dict

values = model_to_dict(model_instance)
for k, v in request.POST.copy().items():
    values[k] = v

form = FooForm(data = values, instance = model_instance)
model_instance = form.save()


My earlier thoughts that model_to_dict wasn't returning the correct
data types was wrong. In fact model_to_dict works fine and looking at
the ticker here http://code.djangoproject.com/ticket/5126 it seems was
created for this exact purpose.

The next issue was updating the model dictionary with the POST values
which, as has been said, are not a real dictionary. Seems I could just
do values.update(request.POST) as it messed the dictionary up. So
instead I did the for loop which is pretty much what I thought
dictionary.update did.

That then all works just fine.

Thanks again.
- Tom


On Feb 16, 10:35 pm, "pjrhar...@gmail.com" <pjrhar...@gmail.com>
wrote:
> > values = model_to_dict(instance)
> > values.update(request.POST)
>
> > It then fails on form.is_valid() because it's trying to match a date
> > string in a date value that is actually a list with one value in it
> > instead of a date string!
>
> I can see two problems here. Firstly, the post "dictionary" isn't a
> real dictionary. In a post string there can be two values to one key,
> eg "foo=1,bar=2,foo=3". When you cast the post "dictionary" to a real
> python dictionary it will be represented as:
> {'foo': [1, 3,], 'bar': [2,]}
>
> So thats why your model instance ends up with lists in it.
>
> The second thing is that if a date is in the post dictionary, it will
> still be a string. When you update your model dictionary you never
> cast it into a python datetime object which is what would be expected.
>
> What I think you want to do is something like this:
>
> form = FooForm(instance=model_instance, data=request.POST)
> model_instance = form,save(commit=False)
>
> Peter

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to