Reputation: 31
I am trying to run a app which has a basic template which accepts the fields and saves it. My Model looks like this:
from django.db import models
# Create your models here.
class Book(models.Model):
"""docstring for MyApp"""
title = models.CharField(max_length=200)
abstract = models.TextField()
pub_date = models.DateTimeField('date published')
publisher = models.CharField(max_length=200)
def __unicode__(self):
return self.title
class Author(models.Model):
name = models.CharField(max_length=200)
sci='Sci'
arts= 'Arts'
engg= 'Engg'
mgmt='mgmt'
none=' '
Choice_In_Intrest = (
(sci,'Science'),
(arts,'Arts'),
(engg,'Engineering'),
(mgmt,'Mangaement'),
)
intrest = models.CharField(max_length=4 ,choices= Choice_In_Intrest , default=none)
book = models.ForeignKey(Book, null=True)
urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
#url(r'^admin/', include(admin.site.urls)),
url(r'^create/$', 'myBook.views.insertBook'),
url(r'^all/$', 'myBook.views.books'),
url(r'^get/(?P<book_id>\d+)/$', 'myBook.views.book'),
url(r'^addAuthor/(?P<book_id>\d+)/$', 'myBook.views.addAuthor'),
]
forms.py:
from django import forms
from models import Book, Author
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields ='__all__'
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
**insert_book.html:**
{% block content%}
<h2>Add Book</h2>
<form action='/create/' method="POST">{%csrf_token%}
<ul>
{{form.as_ul}}
</ul>
<input type="submit" name="submit" value="Add Book">
</form>
{% endblock %}
When I runs the server the Html page displayed, but when I click the Add Book button, it shows the following error:
IntegrityError at /create/
NOT NULL constraint failed: myBook_book.author_id
Request Method: POST
Request URL: http://127.0.0.1:8080/create/
Django Version: 1.8.5
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: myBook_book.author_id
Exception Location: /home/rizwan/django-rizwan/local/lib/python2.7/site-packages/Django-1.8.5-py2.7.egg/django/db/backends/sqlite3/base.py in execute, line 318
Python Executable: /home/rizwan/django-rizwan/bin/python
Python Version: 2.7.6
Python Path:
['/home/rizwan/projects/bookInfo',
'/home/rizwan/django-rizwan/local/lib/python2.7/site-packages/Django-1.8.5-py2.7.egg',
'/home/rizwan/django-rizwan/lib/python2.7/site-packages/Django-1.8.5-py2.7.egg',
'/home/rizwan/django-rizwan/lib/python2.7',
'/home/rizwan/django-rizwan/lib/python2.7/plat-x86_64-linux-gnu',
'/home/rizwan/django-rizwan/lib/python2.7/lib-tk',
'/home/rizwan/django-rizwan/lib/python2.7/lib-old',
'/home/rizwan/django-rizwan/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/rizwan/django-rizwan/local/lib/python2.7/site-packages',
'/home/rizwan/django-rizwan/lib/python2.7/site-packages']
Server time: Wed, 4 Nov 2015 11:12:00 +0000
I haven't define author_id any where, What caused it?
I am using Sqlite 3.
Upvotes: 3
Views: 7388
Reputation: 716
id
fields are automatically generated by Models, and they are predictably of type serial NOT NULL PRIMARY KEY,
. While using the Book model, they are required but have not been set yet. Database didn't get any author_id, so it attempted to set null on this field, but id field has the not null constraint. You can fix by either setting null=True on field definition in the model, or you can have a default, or add an author field to your form.
Upvotes: 3