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.

3
  • 1
    I might be a bit rusty on forms, but why are you passing a User object into the UserPrivacy form? Wouldn't you normally pass in a UserPrivacy instance? Commented Mar 10, 2009 at 18:51
  • i want to update items based on the logged in user. Commented Mar 10, 2009 at 19:12
  • But how do the fields in the User model map to the fields in the UserPrivacy model? Are you just trying to set the user field of the UserPrivacy form? Commented Mar 10, 2009 at 19:15

1 Answer 1

8

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) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.