So, this question has been asked multiple times, but I'm having a hard time applying those solutions to my app.
Models:
class User(models.Model):
name = models.CharField(max_length=45)
alias = models.CharField(max_length=45)
email = models.EmailField()
password = models.CharField(max_length=45)
objects = UserManager()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=45)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=45)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
author = models.ForeignKey(Author)
user = models.ManyToManyField(User, related_name='books')
def __str__(self):
return self.title
class Review(models.Model):
content = models.TextField()
rating = models.IntegerField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
user = models.ManyToManyField(User, related_name='reviews')
book = models.ManyToManyField(Book, related_name='reviews'
Views, the error pops up at the first except when trying to create a new author.
def add_book(request):
if request.method == 'POST':
user = User.objects.get(pk=request.session['user_id'])
data = {
'title':request.POST['title'],
'content':request.POST['review'],
'rating':request.POST['rating'],
}
#Create new author or add author
author_name = request.POST['new_author']
print 'About to see if a new author was provided'
if len(author_name) > 0:
try:
print ' Trying to get existin author '
author = Author.objects.get(name=author_name)
except:
print 'Trying to create a new author'
Author.objects.create(name=author_name)
else:
print 'About to set author to existing author '
author_name = request.POST['author']
author = Author.objects.get(name=author_name)
print 'Author is ', author
#Create book entry
try:
book = Book.objects.get(name=data['title'])
except:
book = Book.objects.create(name=data['title'], author=author, user=user)
print 'New book added'
return redirect('/books')
Error:
IntegrityError at /add_book
book_app_author.book_id may not be NULL
Request Method: POST
Request URL: http://127.0.0.1:8000/add_book
Django Version: 1.11.1
Exception Type: IntegrityError
Exception Value:
book_app_author.book_id may not be NULL
Exception Location: C:\Users\kamar\Desktop\DojoAssignments\django\djangoEnv\djangoEnv\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 328
Python Executable: C:\Users\kamar\Desktop\DojoAssignments\django\djangoEnv\djangoEnv\Scripts\python.exe
Python Version: 2.7.10
Python Path:
['C:\\Users\\kamar\\Desktop\\DojoAssignments\\django\\new_belt',
'C:\\windows\\SYSTEM32\\python27.zip',
'C:\\Users\\kamar\\Desktop\\DojoAssignments\\django\\djangoEnv\\djangoEnv\\DLLs',
'C:\\Users\\kamar\\Desktop\\DojoAssignments\\django\\djangoEnv\\djangoEnv\\lib',
'C:\\Users\\kamar\\Desktop\\DojoAssignments\\django\\djangoEnv\\djangoEnv\\lib\\plat-win',
'C:\\Users\\kamar\\Desktop\\DojoAssignments\\django\\djangoEnv\\djangoEnv\\lib\\lib-tk',
'C:\\Users\\kamar\\Desktop\\DojoAssignments\\django\\djangoEnv\\djangoEnv\\Scripts',
'C:\\Python27\\Lib',
'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk',
'C:\\Users\\kamar\\Desktop\\DojoAssignments\\django\\djangoEnv\\djangoEnv',
'C:\\Users\\kamar\\Desktop\\DojoAssignments\\django\\djangoEnv\\djangoEnv\\lib\\site-packages']
Server time: Mon, 29 May 2017 17:08:42 +0000