仅用于用户 django 的评论模型(1048,“Column 'user_id' cannot be null” or no user filled)

Comment model only for user django (1048, “Column 'user_id' cannot be null” or no user filled)

希望你一切都好

我正在尝试使用一个表单域(内容)为用户创建评论。我想让用户字段自动填写用户值。

我。我从这个模型开始,但出现错误 (1048, “Column 'user_id' cannot be null”): models.py

class Comment(models.Model):
    post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name="comments")
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    content = models.TextField(max_length=160)
    publishing_date = models.DateField(auto_now_add=True)
     
    def __str__(self):
        return self.post.title

views.py

class PostDetail(generic.DetailView,FormMixin):
    model = Post
    context_object_name = 'post'
    template_name = 'post_detail.html'
    form_class = CreateCommentForm
    
    def get_context_data(self, **kwargs):
        context = super(PostDetail, self).get_context_data(**kwargs)
        context['form'] = self.get_form()
        return context

    def form_valid(self, form):
        if form.is_valid():
            form.instance.post = self.object
            form.save()
            return super(PostDetail, self).form_valid(form)
        else:
            return super(PostDetail, self).form_invalid(form)

    def post(self,*args,**kwargs):
        self.object = self.get_object()
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_valid(form)

    def get_success_url(self):
        return reverse('post_detail',kwargs={"slug":self.object.slug})

forms.py

class CreateCommentForm(forms.ModelForm):
    def __init__(self,*args,**kwargs):
        super(CreateCommentForm, self).__init__(*args,**kwargs)
        self.helper = FormHelper()
        self.helper.form_method="post"
        self.helper.layout = Layout(
            Field("content",css_class="form-control",style="margin-bottom:10px",rows="1"),
        )
        
        self.helper.add_input(Submit('submit','Comment',css_class="btn btn-sm",style="background-color: #0d6ec5;border-color: #0d6ec5;"))



    class Meta:
        model = Comment
        fields = [
            'content'
        ]

二.它在我的管理面板上有效,但在我的网站上无效(我收到此错误:(1048,“列 'user_id' 不能为空”)。 所以我决定改变我的模型:

class Comment(models.Model):
    post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name="comments")
    user = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE)
    content = models.TextField(max_length=160)
    publishing_date = models.DateField(auto_now_add=True)
     
    def __str__(self):
        return self.post.title

问题是,如果用户 post 发表评论,内容没问题,但没有与该评论相关的用户。有人有想法吗?

您需要指定 .user 属性,可能是登录用户:

from django.contrib.auth.mixins import LoginRequiredMixin

class PostDetail(LoginRequiredMixin, FormMixin, generic.DetailView):
    model = Post
    context_object_name = 'post'
    template_name = 'post_detail.html'
    form_class = CreateCommentForm
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['form'] = self.get_form()
        return context

    def form_valid(self, form):
        form.instance.post = self.object
        form<b>.instance.user = self.request.user</b>
        return super().form_valid(form)

    def post(self,*args,**kwargs):
        self.object = self.get_object()
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_valid(form)

    def get_success_url(self):
        return reverse('post_detail',kwargs={"slug":self.object.slug})

Note: You can limit views to a class-based view to authenticated users with the LoginRequiredMixin mixin [Django-doc].


Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.