Hi all. I have a model form to update two attributes on a UserProfile.

The form is simple:

class UserProfileSetRememberMeForm(forms.ModelForm):
  DURATION_CHOICES = ( 
      (  0, 'Default (2 weeks)'),
      (  1 * 7 * 24 * 60 * 60, '1 week'),
    ) 
  YESNO_CHOICES = ( (0, 'No'), (1, 'Yes'), )
  remember_me_duration = forms.TypedChoiceField(choices=DURATION_CHOICES, 
                                                required=False, 
                                                coerce=smart_int)
  remember_me = forms.ChoiceField(choices=YESNO_CHOICES, required=False)
  
  def __init__(self, *args, **kwargs):
    super(forms.ModelForm, self).__init__(*args, **kwargs)
    attrs = { 'class': 'remember_me', }
    self.fields['remember_me'].widget.attrs = dict(attrs)
    if 'instance' in kwargs and not kwargs['instance'].remember_me:
      attrs['disabled'] = 'disabled'
    self.fields['remember_me_duration'].widget.attrs = dict(attrs)

  class Meta:
    model = UserProfile
    fields = ( 'remember_me', 'remember_me_duration' )

The two fields are defined like so:
  remember_me = models.BooleanField()
  remember_me_duration = models.IntegerField(default=0)

The view is similarly simple:

def update_user_remember_me(request, user_id=None):
  user = get_object_or_404(User, id=user_id)
  prof = user.get_profile()
  print "current: pk: %d  remember_me: %s  duration: %d" \
      % (prof.pk, prof.remember_me, prof.remember_me_duration)
  json = { 'valid': False }
  if request.method == 'POST':
    frm = UserProfileSetRememberMeForm(request.POST, instance=prof)
    if frm.is_valid():
      p2 = frm.save(commit=False)
      print "to save: pk: %d  remember_me: %s  duration: %d" \
          % (prof.pk, prof.remember_me, prof.remember_me_duration)
      p2.save()
      prof = UserProfile.objects.get(pk=prof.pk)
      print "from db: pk: %d  remember_me: %s  duration: %d" \
          % (prof.pk, prof.remember_me, prof.remember_me_duration)
      json['enabled'] = prof.remember_me == 1
      json['valid'] = True
  return JsonResponse(json)

The debug output shows that (as far as I can see), the model should be
saved and the DB updated:

current: pk: 1  remember_me: 1  duration: 0
to save: pk: 1  remember_me: 0  duration: 0
from db: pk: 1  remember_me: 1  duration: 0

However, as you can see, it is not updated. Calling
'p2.save(force_update=True)' results in a 'Database error: Forced update
did not affect any rows' exception.

Any ideas? This is flummoxing me :/

Cheers

Tom


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to