开发者

Django form error

class JobForm(forms.ModelForm):
    class Meta:
        model = models.Job

That was my form, now trying to save it will raise an exception, and trying to validate it just fails with no errors....

job = get_object_or_404(models.Job, pk=1)
form = forms.JobForm(instance = job)
try:
    form.save()
except:
    print sys.exc_info()

#=>(<type 'exceptions.AttributeError'>, AttributeError("'JobFo开发者_如何学编程rm' object has no attribute 'cleaned_data'",), <traceback object at 0x1029dbb48>)

Tried to validate it:

if form.is_valid():
    form.save()
else:
    print 'error'
    print form.errors, len(form.errors)
#=> 'error' 
#=> 0

So the form isn't valid, but the there are no errors! Any idea?


Your form is definitely not bound. Read about Bound and Unbound forms.

From that documentation:

To bind data to a form, pass the data as a dictionary as the first parameter to your Form class constructor.

That means that also the change of a field in your model doesn't make the form bound. You have to pass those values explicitly via the constructor. But:

Note that passing an empty dictionary creates a bound form with empty data

and consider this

If you have a bound Form instance and want to change the data somehow, or if you want to bind an unbound Form instance to some data, create another Form instance. There is no way to change data in a Form instance. Once a Form instance has been created, you should consider its data immutable, whether it has data or not.

If you validate an unbound form:

It's meaningless to validate a form with no data, but, for the record, here's what happens with unbound forms:

>>> f = ContactForm()
>>> f.is_valid()
False
>>> f.errors
{}


My best guess is that you can't save a model form that has only been created with instance data (I just tried doing this with one of my forms and got the exact same error) because a form doesn't become a bound form until you pass in extra information. It doesn't really make sense to only provide data that already exists on the model to a form (and then save it again) as there wouldn't be any change. If this is coming from an html form, you should also be passing in request.POST (or request.GET) to the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜