【问题标题】:django integrityerror appending _id to the end of my attribute namedjango 完整性错误将 _id 附加到我的属性名称的末尾
【发布时间】:2012-05-06 18:37:15
【问题描述】:

我已经阅读了一些关于将 _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 的外键,我想将其设置为用户食谱的初始值。

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

谢谢

小鱼

【问题讨论】:

    标签: django forms foreign-keys integrity


    【解决方案1】:

    .....

    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()
    

    【讨论】:

    • 这似乎有效,虽然我相信我还有其他错误发生谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-09-06
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 2015-04-11
    • 1970-01-01
    相关资源
    最近更新 更多