Sunday, January 17, 2010

Editing ModelForm instance

When editing an object using ModelForm, the best way I know is

def my_view(request):
obj_id = request.POST.get('obj_id', '')
old_object = MyModel.objects.get(pk=obj_id)
form = MyModelForm(request.POST, instance=old_object)
...

but I'm thinking the first two lines are a pain to write every time.
Is it possible to do it all in one line? Something like where
MyModelForm is smart enough to put it's own hidden field in the html
to store the pk, and then when loading check the request.POST to see
if the hidden field contains a pk so it can know if it should be an
instance or not.

in short, is there a better way to do this, or am I using the best
Django's got? hope I make sense...

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate