Sunday, 20 November 2011

How to Save and Update using Form using django

if you have used django forms and have inherited them using any Model which actually refer to table in database. on posting you could easly parse form like:

frm = CharacterForm(request.POST)

where CharacterForm is your Form which might inherit fields from Character Model. and Now you could Save this new Record using simple frm.save()

How about Update:

Some time you need to update record. to do this you will be required to bound that record to Model instance to do this update above form parsing line to:
frm = CharacterForm(request.POST, instance = Character.objects.get(pk = idofrecordtoupdate))

then simple use

frm.save()

this will update existing record instead of inserting new record.

Best of luck.

No comments:

Post a Comment