0

我已经阅读了一些关于将 _id 附加到属性末尾引起的完整性错误的示例,但其中大多数都认为 user_id 这不是我遇到的错误 - 我正在处理将外键的初始值设置为另一个模型(食谱有 fk 到食谱)

所以这是我的踪迹

IntegrityError at /cookbook/createrecipe/

(1048, "Column 'original_cookbook_id' cannot be null")

Request Method:     POST
Request URL:    http://127.0.0.1:8000/cookbook/createrecipe/
Django Version:     1.3.1
Exception Type:     IntegrityError
Exception Value:    

(1048, "Column 'original_cookbook_id' cannot be null")

Exception Location:     /Library/Python/2.6/site-packages/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg/MySQLdb/connections.py in defaulterrorhandler, line 36
Python Executable:  /usr/bin/python
Python Version:     2.6.1
def createrecipe(request):

        form = RecipeForm(request.POST)

        if request.method == 'POST':

            if form.is_valid():

                form = RecipeForm(initial = {'original_cookbook' : request.user.cookbooks.all()[0]})

                recipe = form.save()//error

...

            t = loader.get_template('cookbook/create_form.html')

            c = RequestContext(request, {

                'form': form,

            })

            data = {

            'form': t.render(c),

正如您可能看到的那样,我有一个名为 original_cookbook 的外键,我想将其设置为用户的食谱的初始值。

知道可能是什么原因,如果需要,我可以发送更多代码

谢谢

零食鱼

4

1 回答 1

0

......

form = RecipeForm(request.POST)
if request.method == 'POST':
  if form.is_valid():
    recipe = form.save(commit=False)
    recipe.original_cookbook = request.user.cookbooks.all()[0]
    recipe.save()
于 2012-04-26T04:33:43.593 回答