views:

1111

answers:

1

I have a model as follows

class UserPrivacy(models.Model):
    user = models.ForeignKey(User)
    profile = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE)
    contact = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE)
    friends = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE)
    location = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE)

My modelform is as follows

class PrivacyForm(ModelForm):
    class Meta:
        model = UserPrivacy
        exclude = ('user','location')

My function looks like this to display and update the form.

def show_privacy(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/')

    if request.method == 'POST':
        form = PrivacyForm(request.POST, instance=User.objects.get(pk=request.session['id']))
        if form.is_valid():
            form.save()

    else:
        form = PrivacyForm()

    return render_to_response('settings_privacy.html', {'form': form}, context_instance=RequestContext(request))

My user_id in the db is 1.. but when I post a form it never gets updated. I know form.save() gets called due to putting a print there and it gets shown in the dev server.

+2  A: 

Andy Hume was correct in the comments to your question.

You have a ModelForm based on the UserPrivacy model, yet you are passing it an instance of User.

What you want to do is this:

form = PrivacyForm(request.POST, instance=UserPrivacy.objects.get(user=request.user)
Van Gale