获得一篇文章django python的作者

时间:2014-12-01 14:43:13

标签: django post author

我试图获取每个帖子的作者,所以我在views.py文件夹中写了这个函数,允许你添加帖子,除非我介绍post_form = poster我得到错误

NOT NULL约束失败:blog_post.author_id

这是addPost函数:

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)

以下是相关型号:

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

感谢所有

这是错误:

NOT NULL约束失败: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

2 个答案:

答案 0 :(得分:2)

我认为你需要这样的东西来避免你的错误:

if post_form.is_valid():
    new_post = post_form.save(commit=False)
    new_post.author = poster
    new_post.save()

答案 1 :(得分:1)

我认为您的问题出在您的作者实例中。您正在尝试创建表单,将其设置为值,将其添加为作者并保存。 首先,表单没有作者字段,您将其用以下行排除:

fields = ('title', 'body', 'category')

尝试类似this的内容:

if post_form.is_valid():
            postAdded = True
            instance = post_form.save(commit=False)
            instance.author = poster
            instance.save()

这样您就不会为作者字段设置NULL值。