On Fri, 2009-09-18 at 15:29 +0100, Tom Evans wrote: > On Fri, 2009-09-18 at 06:53 -0700, Daniel Roseman wrote: > > On Sep 18, 1:01 pm, Tom Evans <tevans...@googlemail.com> wrote: > > > 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()
It is definitely related to type coercion: if I just change remember_me to be a PositiveSmallIntegerField, the exact same code works perfectly. I would really like to know what is wrong with my code when it is a BooleanField, but this work around stops me swearing at my computer incessantly :) I also just noticed I never mentioned this is with django 1.0 . > > > 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 > > > > I don't think that code can be producing the output you give. The line > > where you print the 'to save' information is getting it from the > > original 'prof' object, not the new 'p2' object from form.save(), so > > the output should be the same. > > > > Can you post the actual code that you're running? > > -- > > DR. > > Yep, I noticed that shortly after sending it. The output is the same if > I change the 'to save' line to print out from p2 rather than prof. > > I'm trying to trace it through pdb, but its slow going. I think it might > be related to type coercions.. > > Here is the exact code I'm running at the moment in the view, and the > output it generates. I thought to have a look at connection.queries, but > it never issues one, so that isn't very interesting either :/ > > def update_user_remember_me(request, user_id=None): > from django.db import connection > connection.queries=[] > 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) > p2.save() > print "to save: pk: %d remember_me: %s duration: %d" \ > % (p2.pk, p2.remember_me, p2.remember_me_duration) > 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) > print connection.queries > json['enabled'] = prof.remember_me == 1 > json['valid'] = True > return JsonResponse(json) > > > 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 > [] > > > 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 django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---