django integrityerror appending _id to the end of my attribute name
-
03-06-2021 - |
Question
i have read a few of the examples regarding integrirty errors caused by appending _id to the end of an attribute but most of them regard user_id which is not the error i have - mine is dealing with a setting the initial value of a foreign key to another model (recipe has fk to cookbook)
so here is my trace
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),
as you can probably see i have a foreign_key named original_cookbook that i want to set initial to the cookbook of the user.
any idea what might be causing this i can send more code if needed
thanks
snackerfish
Solution
.....
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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow