On Feb 3, 5:45 pm, pjmorse <flashesofpa...@gmail.com> wrote:
> I'm trying to sort out a problem in the console with editing an
> object. The user-reported problem, which I verified, is that changes
> made in the edit form are not made in the database; the user gets no
> error message, but their changes aren't made, either.
>
> I narrowed the problem down to form validation in the view, and using
> pdb and some debug logging commands I got the validation errors out.
> Here's the problematic code:
>
>         if request.method == 'POST':
>                 data = request.POST.copy()
>                 import pdb; pdb.set_trace() # My debugger call
>
>                 a = AthleteForm(data,instance=athlete_obj)
>                 pf = AthleteProfileForm(data,instance=athleteprofile)
>
> Now, in the debugger:
>
> -> a = AthleteForm(data,instance=athlete_obj)
> (Pdb) n> /Users/morse/Sites/wmm/console/athletes/views.py(538)athletes_edit()
>
> -> pf = AthleteProfileForm(data,instance=athleteprofile)
> (Pdb) n
> (Pdb) pf.is_valid()
> False
> (Pdb) pf.errors
> {'athlete': [u'This field is required.'], 'language': [u'This field is
> required.']}
> (Pdb) athleteprofile.language
> <Language: US (US)>
> (Pdb) athleteprofile.athlete
> <Athlete: Martin Lel>
>
> So I've established that the AthleteProfile object the
> AthleteProfileForm is bound to has these values set, but the
> AthleteProfileForm does not. Does this mean the fields are missing
> from the "data" (and therefore from the request.POST)?
>
> (Pdb) pf.base_fields
> {'athlete': <django.forms.models.ModelChoiceField object at
> 0x15b05b0>, 'language': <django.forms.models.ModelChoiceField object
> at 0x13a8050>, 'pbest': <django.forms.fields.CharField object at
> 0x13d4c10>, 'highlights': <django.forms.fields.CharField object at
> 0x13d4910>, 'career_notes': <django.forms.fields.CharField object at
> 0x13d45b0>, 'personal_notes': <django.forms.fields.CharField object at
> 0x13d4e70>, 'additional_career_highlights':
> <django.forms.fields.CharField object at 0x13d4ed0>,
> 'other_personal_bests': <django.forms.fields.CharField object at
> 0x13d4f30>, 'upcoming_marathons': <django.forms.fields.CharField
> object at 0x13d4f90>, 'wmm_highlights': <django.forms.fields.CharField
> object at 0x13d4ff0>, 'translated': <django.forms.fields.BooleanField
> object at 0x13e2070>}
>
> And, edited for brevity:
>
> (Pdb) data
> <QueryDict: {u'other_personal_bests': [u''], u'translated': [u'on'],
> u'surname': [u'Lel'], u'name': [u'Martin'], u'dob': [u'1978-10-31'],
> u'gender': [u'm'], u'image': [u'wmm_2546834213413.jpg'],
> u'additional_career_highlights': [u'27Oct02  Venice Marathon  2nd
> 2:10:02'], u'wmm_highlights': [u'[string omitted]'], u'pbest':
> [u'2:05:15 (London, 2010)'], u'career_notes': [u"[string omitted]"],
> u'personal_notes': [u"[string omitted]"], u'user': [u'2'],
> u'highlights': [u''], u'y': [u'14'], u'x': [u'14'], u'country':
> [u'114'], u'upcoming_marathons': [u'']}>
>
> The DOB and 'pbest' fields are blatantly false, data I submitted to
> establish that nothing was changing in the database. Notice that some
> of the fields are part of the Athlete model and not the AthleteProfile
> model.
>
> How can I give the AthleteProfileForm correct values (either in the
> form for data submission, or directly in the view code) so it will
> validate? Also, is there another debugging method I can use to better
> reveal what's going wrong here?
>
> I've looked through the list archives for the last year or so and
> haven't found anything similar; I fear that's because it's so obvious
> everyone else is figuring it out for themselves. The site is running
> on Django 1.0.2 due to decisions made before my involvement with the
> project (and part of my level of frustration here is that I've never
> seen this particular code working correctly, which is making it very
> hard for me to understand what's going wrong.)
>
> Thanks,
>
> pjm

You don't show the code of the Athlete or AthleteProfile forms. Are
they just standard model forms, with no excluded fields?

Secondly, is the language field actually displayed on the HTML
template for the athleteprofile form? It's significant that it's not
in the POSTed data, which would seem to indicate that you haven't
included the field in the template. If you've left it out for a
reason, you should also exclude it from the form, by adding it to the
'exclude' tuple in the form's inner Meta class, so that it doesn't
prevent validation.
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.

Reply via email to