views:

51

answers:

1

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

+2  A: 

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.

unclaimedbaggage
thanks a lot, i'll try this (i was doing it without the instance=user_cv by now:D)
dana
dana, please whenever one of your questions is answered for you, consider accepting it by clicking the checkmark on the left of it. that's what encourages people to answer on stackoverflow.
mawimawi
:) i've clicked now! i'm new here, and i didn't know:) thanks for telling me:)
dana