0

Working out editing profiles and ran into a little trouble! The following piece of code succesfully updates the mug_shot column in the user profile, but also erases all the other column data for that particular record. It's weird because Django is supposed to automatically distinguish between updates/saves. What's weirder is that everywhere else updates/saves seem to work fine.

I'm kind of at a loss.

@login_required
def add_mugshot(request):
    user = request.user
    profile = UserProfile.objects.get(user=user)
    if request.method == 'POST':
        profile_form = ProfileForm(request.POST, request.FILES, instance=profile)
        if profile_form.is_valid():
            new_profile = profile_form.save(commit=False)
            new_profile.user = user
            new_profile.save()

            return HttpResponseRedirect('/accounts/profile/')
    else:
        profile_form = ProfileForm(instance=profile)

    return render_to_response('accounts/add_mugshot.html', 
        RequestContext(request, {
            'profile_form': profile_form}))

1 Answers1

0

I wonder if this is something in your form template. If you aren't showing all of the fields, Django will interpret them as empty, and save your instance with empty fields.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • That's the best explanation I've been able to come up with, however, my edit_profile(request) code all works. I can update the profile without the mugshot column disappearing. However, if I update the mugshot, the rest of the record data disappears. –  Aug 27 '09 at 01:39