0
當我嘗試加載我的類別時出現以下錯誤。發佈的值是「1」和存在於DB,int()參數必須是一個字符串或數字,而不是'類'
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,所以Django auto是否會爲我或某物獲取對象? – Prometheus
還沒有與表格工作過很多,但我猜測它會自動傳遞id(因爲你如何將一個對象作爲輸入值),然後在服務器端準確解析它 – Colleen
。在窗體中使用ForeignKeyField時,django使用返回相關對象的pk(主鍵)字段的窗口小部件。 forms.Form類會自動返回相關對象。 – furins