I'm trying to get the author of each post, so I've written this function in the views.py folder that allows you to add a post, except when I introduce post_form = poster I get errors
NOT NULL constraint failed: blog_post.author_id
Here is the addPost function:
def addPost(request):
context = RequestContext(request)
postAdded = False
poster = request.user
if request.method == 'POST':
post_form = PostForm(data=request.POST)
if post_form.is_valid():
postAdded = True
post_form.author = poster
post_form.save()
else:
print(post_form.errors)
else:
post_form = PostForm()
return render_to_response(
'Forum/addPost.html',
{'post_form': post_form, 'postAdded': postAdded},
context)
and here are the relevant models:
class UserProfile(models.Model):
user = models.OneToOneField(User)
class Post(models.Model):
author = models.ForeignKey(User, related_name='poster')
class PostForm(forms.ModelForm):
class Meta:
# Provide an association between the ModelForm and a model
newPost = Post
# newPost.category = check which url we are in
model = newPost
fields = ('title', 'body', 'category')
Thanks to all
Here is the error:
NOT NULL constraint failed: blog_post.author_id
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/nicolasanastassacos/Desktop/DjangoProject/blog/views.py" in addPost
80. post_form.save()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in save
457. construct=False)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/forms/models.py" in save_instance
103. instance.save()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in save
590. force_update=force_update, update_fields=update_fields)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in save_base
618. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in _save_table
699. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py" in _do_insert
732. using=using, raw=raw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
92. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/query.py" in _insert
921. return query.get_compiler(using=using).execute_sql(return_id)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
920. cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
81. return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/utils/six.py" in reraise
549. raise value.with_traceback(tb)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py" in execute
485. return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /Forum/addPost/
Exception Value: NOT NULL constraint failed: blog_post.author_id
I think your issue is at your author instance. You are trying to create a form, setting it the values, adding it an author and saving it. First, the form does not have an author field, you are excluding it with the line:
Try something like this:
That way you are not setting a NULL value to your author field.