int() 参数必须是字符串或数字,而不是 'Category
我在尝试加载我的分类时遇到了下面的错误。提交的值是'1',而且这个值在数据库中是存在的。
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 个回答
1
听起来通过 form.cleaned_data['category']
得到的对象是一个分类对象,而不是一个数字。
如果是这样的话,你只需要写 category = form.cleaned_data['category']
。
可以打印出来确认一下。