On Feb 3, 2:10 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Feb 3, 5:45 pm, pjmorse <flashesofpa...@gmail.com> wrote:

> > 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:
> > (Pdb) pf.errors
> > {'athlete': [u'This field is required.'], 'language': [u'This field is
> > required.']}

> 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.

Thanks, Daniel, good questions. The second one - that the language
wasn't actually in the field - turned out to be the key; the language
is set based on the user session inside the view (code below) but when
I followed your idea and used hidden form fields to add the athlete
and language IDs to the form, everything got assigned appropriately in
the view and validation passed.

The form code is so simple I didn't think it was relevant:

class AthleteForm(forms.ModelForm):

        class Meta:
                model = Athlete

class AthleteProfileForm(forms.ModelForm):

        class Meta:
                model = AthleteProfile

These are the associated models (I've trimmed commented code for
brevity):

class Athlete(models.Model):
        def __unicode__(self):
                return '%s %s' % (self.name, self.surname)

        name = models.CharField(max_length=255)
        surname = models.CharField(max_length=255)
        country = models.ForeignKey(Country)
        dob = models.DateField()
        gender = models.CharField(max_length=1,choices = (('m','Male'),
('f','Female')))

        user = models.ForeignKey(AdminUser,
related_name='athleter_owner_set', verbose_name = 'Creator')
        modified = models.ForeignKey(AdminUser, blank = True, null = True,
related_name='athlete_modified_set', verbose_name='Modified by')

        deleted = models.BooleanField(default = False)

        date_created = models.DateTimeField(null = True, blank = True,
default = datetime.now)
        date_modified = models.DateTimeField(null = True, blank = True,
default = datetime.now)

        image = models.CharField(max_length = 255, null = True, default =
'GENERIC.jpg')


class AthleteProfile(models.Model):

        def __unicode__(self):
                return '%s %s' % (self.athlete.name, self.athlete.surname)

        athlete = models.ForeignKey(Athlete)
        language = models.ForeignKey(Language)

        pbest = models.CharField("Personal
best",max_length=255,blank=True ,null = True, default = '00:00:00')
        highlights = models.TextField(null = True, blank=True, default = '')
        career_notes = models.TextField(blank=True ,null = True, default =
'')
        personal_notes = models.TextField(blank=True ,null = True, default =
'')
        additional_career_highlights = models.TextField(blank=True ,null =
True, default = '')
        other_personal_bests = models.TextField(blank=True ,null = True,
default = '')
        upcoming_marathons = models.TextField(blank=True ,null = True,
default = '')
        wmm_highlights = models.TextField("WMM highlights",blank=True ,null =
True, default = '')
        translated = models.BooleanField(default = False ,null = True, blank
= True)

It's not clear to me why the original programmer chose to normalize
the database this way, but I think their intention was to separate
translatable fields. "Language" is not displayed in the HTML template;
it's set in the view a few lines before the code I included, and based
on the user submitting the form:

def athletes_edit(request, athlete_id):
        language = get_object_or_404(Language, locale =
request.session['adminlangID'])

        """
        Get current athlete object
        """

        athlete_obj = get_object_or_404(Athlete, pk = athlete_id)

        athleteprofile, created =
AthleteProfile.objects.get_or_create(athlete = athlete_obj, language =
language)

My assumption was that when the AthleteProfileForm was created from
`athleteprofile` (which is an AthleteProfile object) that the language
value would be maintained, but apparently such is not the case.

Thanks again, Daniel, this has been a thorn in my side for several
days and now I've closed it up.

pjm

-- 
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