int()参数必须是字符串或数字,而不是'类别'

时间:2013-02-01 23:28:54

标签: python django

尝试加载我的类别时,我收到以下错误消息。发布的值为“1”并存在于DB中,

TypeError at /events/add/

int() argument must be a string or a number, not 'Category'

Request Method:     POST
Request URL:    http://127.0.0.1:8000/events/new/
Django Version:     1.4
Exception Type:     TypeError
Exception Value:    

int() argument must be a string or a number, not 'Category'

Exception Location:     /Library/Python/2.7/site-packages/django/db/models/fields/__init__.py in get_prep_value, line 537
Python Executable:  /Users/user/Documents/workspace/RoseBud/django-env/bin/python

views.py

 category = Category.objects.get(pk=form.cleaned_data['category'])

1 个答案:

答案 0 :(得分:1)

听起来像form.cleaned_data['category']返回的对象是类别对象,而不是数字。

如果是这种情况,请执行category = form.cleaned_data['category']

打印出来以确定。