pythondjangodjango-forms

Django - Form not valid but no error


Model:

class Session(models.Model):
    tutor = models.ForeignKey(User)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    def __unicode__(self):
        return u'%s - %s' % (self.tutor, self.start_time)

Form:

class SessionForm(forms.ModelForm):
    class Meta:
        model = Session
        exclude = ['tutor']

Testing:

>>> ses = Session.objects.get(pk=1)
>>> ses
<Session: Robert - 2012-03-22 13:00:00>
>>> form = SessionForm(instance=ses)
>>> form.is_valid()
false
>>> form.errors
{}

What's wrong ?


Solution

  • You have not passed any data to the form, so it is not valid. The instance argument is not used to set the form data, just the initial data.