django forms overwrite data when saved
If a have a form, with the data from a user, let's say a CV, and i save the data from the form into a database, but i don't want that a CV 开发者_如何学编程from the same user to be stored in the database more than once(when edited form instance)
I want it to be overwritten every time it is saved by one same user. How can i do it?
thanks a lot
Django's save() should handle this for you automatically.
To give an example, you'll usually submit a form in a way something like this:
...
form = UserCVForm(request.POST, instance=user_cv)
if form.is_valid():
form.save()
...
'instance=user_cv' tells django that you want to update an existing entry - specifically 'user_cv'. Without 'instance=user_cv', Django will insert a new entry into the database.
So in short, see if a user_cv exists already with something like user_cv = UserCV.objects.get(user=user_id). If a user_cv exists, be sure to whack an instance=user_cv in when populating the form.
精彩评论