简体   繁体   English

django.db.utils.IntegrityError:NOT NULL 约束失败:home_post.author_id

[英]django.db.utils.IntegrityError: NOT NULL constraint failed: home_post.author_id

I have built a form for posting using Ajax, however, when I click on the submit button i get the error:我已经构建了一个使用 Ajax 发布的表单,但是,当我单击提交按钮时,出现错误:

django.db.utils.IntegrityError: NOT NULL constraint failed: home_post.author_id

I do not know what this is as before I had a separate html for creating posts but after using Ajax I am getting this error.我不知道这是什么之前我有一个单独的 html 来创建帖子,但是在使用 Ajax 之后我收到了这个错误。

This is my Post Model:这是我的帖子模型:

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField(validators=[MaxLengthValidator(250)])
    author = models.ForeignKey(Profile, on_delete=models.CASCADE)
    date_posted = models.DateTimeField(auto_now_add=True)
    last_edited= models.DateTimeField(auto_now=True)
    likes= models.ManyToManyField(Profile, blank=True, related_name='post_likes')

This is my post form:这是我的帖子形式:

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title','content',)

This is my create_post view:这是我的 create_post 视图:

@login_required
def post_create(request):
    data = dict()
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():      
            form.save()
            data['form_is_valid'] = True
            posts = Post.objects.all()
            data['posts'] = render_to_string('home/homepage/home.html',{'posts':posts})
        else:
            data['form_is_valid'] = False
    else:
        form = PostForm       
    context = {
    'form':form
    }
    data['html_form'] = render_to_string('home/posts/post_create.html',context,request=request)
    return JsonResponse(data) 

This is my Ajax Javascript code:这是我的 Ajax Javascript 代码:

$(document).ready(function(){
    var ShowForm = function(){
        var btn = $(this);
        $.ajax({
            url: btn.attr("data-url"),
            type: 'get',
            dataType:'json',
            beforeSend: function(){
                $('#modal-post').modal('show');
            },
            success: function(data){
                $('#modal-post .modal-content').html(data.html_form);
            }
        });
    };

    var SaveForm =  function(){
        var form = $(this);
        $.ajax({
            url: form.attr('data-url'),
            data: form.serialize(),
            type: form.attr('method'),
            dataType: 'json',
            success: function(data){
                if(data.form_is_valid){
                    $('#post-list div').html(data.posts);
                    $('#modal-post').modal('hide');
                } else {
                    $('#modal-post .modal-content').html(data.html_form)
                }
            }
        })
        return false;
    }

//create
$('.create-post-btn').click(ShowForm);
$('#modal-post').on("submit",".post-create-form",SaveForm)

//update
$('#post-list').on("click",".show-form-update",ShowForm);
$('#modal-post').on("submit",".update-form",SaveForm)

//delete
$('#post-list').on("click",".show-form-delete",ShowForm);
$('#modal-post').on("submit",".delete-form",SaveForm)
});

I do not know what is causing this error as everything seems right to me.我不知道是什么导致了这个错误,因为在我看来一切都是正确的。

In the model of Post, the author is not null. Post模型中,作者不为空。 But in the view, you didn't set the author before saving the form.但是在视图中,您在保存表单之前没有设置作者。

You can add an existed author id to post in the view.您可以添加现有的作者 ID 以在视图中发布。

post = form.save(False)
post.author = author_id
post.save()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置 post_id 以解决错误:'django.db.utils.IntegrityError:NOT NULL 约束失败:posts_comment.post_id'? - How to set post_id to resolve error : 'django.db.utils.IntegrityError: NOT NULL constraint failed: posts_comment.post_id'? Django Runserver失败,并出现django.db.utils.IntegrityError:NOT NULL约束失败:reviewer_post.post_id(AutoField) - Django runserver fails with django.db.utils.IntegrityError: NOT NULL constraint failed: reviewer_post.post_id (AutoField) Createsuperuser django.db.utils.IntegrityError: NOT NULL 约束失败 - Createsuperuser django.db.utils.IntegrityError: NOT NULL constraint failed django.db.utils.IntegrityError:NOT NULL约束失败 - django.db.utils.IntegrityError: NOT NULL constraint failed django.db.utils.IntegrityError: NOT NULL 约束失败: - django.db.utils.IntegrityError: NOT NULL constraint failed: django.db.utils.IntegrityError: NOT NULL 约束失败来自 Postman - django.db.utils.IntegrityError: NOT NULL constraint failed fom Postman django.db.utils.IntegrityError:NOT NULL 约束失败:users_profile.user_id - django.db.utils.IntegrityError: NOT NULL constraint failed: users_profile.user_id django.db.utils.IntegrityError: NOT NULL 约束失败:app_users.key_value_id - django.db.utils.IntegrityError: NOT NULL constraint failed: app_users.key_value_id django.db.utils.IntegrityError: NOT NULL 约束失败:accounts_user.user_id - django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_user.user_id django.db.utils.IntegrityError:NOT NULL 约束失败:Eid_Post_name.massenger_name - django.db.utils.IntegrityError: NOT NULL constraint failed: Eid_Post_name.massenger_name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM