为什么会出现此错误? UNIQUE 约束失败:app1_comment.user_id
Why am I getting this error? UNIQUE constraint failed: app1_comment.user_id
问题是我正在为我的 post 创建一个评论部分,我想为用户创建的每条评论添加一个编辑按钮,但问题是我只能添加一个每个用户的评论或者我在标题中得到错误,我不知道如何解决这个问题,所以这里是代码
models.py
class Category(models.Model):
name = models.CharField(max_length= 255)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('index')
class Profile(models.Model):
user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
bio = models.TextField()
profile_pic = models.ImageField(null = True, blank = True, upload_to = "images/profile/")
website_url = models.CharField(max_length= 255, blank = True, null = True)
facebook_url = models.CharField(max_length= 255, blank = True, null = True)
twitter_url = models.CharField(max_length= 255, blank = True, null = True)
instagram_url = models.CharField(max_length= 255, blank = True, null = True)
pinterest_url = models.CharField(max_length= 255, blank = True, null = True)
def __str__(self):
return str(self.user)
def get_absolute_url(self):
return reverse('index')
class Post(models.Model):
title = models.CharField(max_length= 255)
header_image = models.ImageField(null = True, blank = True, upload_to = 'images/')
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank = True, null = True)
#body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='coding')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name = 'blog_posts')
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('app1:article-detail', args=(self.id,))
class Comment(models.Model):
user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
name = models.CharField(max_length=255)
body = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s - %s' % (self.post.title, self.name)
views.py
class UpdateCommentView(UpdateView):
model = Comment
template_name = 'app1/update_comment.html'
form_class = CommentForm
def form_valid(self, form):
form.instance.post_id = self.kwargs['pk']
form.instance.user = self.request.user
return super().form_valid(form)
class AddCommentView(CreateView):
model = Comment
form_class = CommentForm
template_name = 'app1/add_comment.html'
def form_valid(self, form):
form.instance.post_id = self.kwargs['pk']
form.instance.user = self.request.user
return super().form_valid(form)
#fields = '__all__'
success_url = reverse_lazy('index')
模板
<div class="container">
<h1>Post</h1>
{% for post in object_list %}
<li><a href="{% url 'app1:article-detail' post.pk %}">{{post.title}}</a> -
{{post.author}} - <small>{{post.post_date}}</small> - <small> category : <a href="{% url 'app1:category' post.category|slugify %}">{{post.category}}</a> - </small>
{% if user.is_authenticated %}
{% if user.id == post.author.id %}
<small><a href="{% url 'app1:updatepost' post.pk %}">Edit</a></small><small>
<a href="{% url 'app1:deletepost' post.pk %}">- Delete</a>
</small></li>
{{post.snippet}}
{% endif %}
{% endif %}
您的评论模型必须是外键而不是 OneToOneField:
class Comment(models.Model):
user = models.ForeignKey(User, null = True, on_delete=models.CASCADE)
...
问题是我正在为我的 post 创建一个评论部分,我想为用户创建的每条评论添加一个编辑按钮,但问题是我只能添加一个每个用户的评论或者我在标题中得到错误,我不知道如何解决这个问题,所以这里是代码
models.py
class Category(models.Model):
name = models.CharField(max_length= 255)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('index')
class Profile(models.Model):
user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
bio = models.TextField()
profile_pic = models.ImageField(null = True, blank = True, upload_to = "images/profile/")
website_url = models.CharField(max_length= 255, blank = True, null = True)
facebook_url = models.CharField(max_length= 255, blank = True, null = True)
twitter_url = models.CharField(max_length= 255, blank = True, null = True)
instagram_url = models.CharField(max_length= 255, blank = True, null = True)
pinterest_url = models.CharField(max_length= 255, blank = True, null = True)
def __str__(self):
return str(self.user)
def get_absolute_url(self):
return reverse('index')
class Post(models.Model):
title = models.CharField(max_length= 255)
header_image = models.ImageField(null = True, blank = True, upload_to = 'images/')
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank = True, null = True)
#body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='coding')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name = 'blog_posts')
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('app1:article-detail', args=(self.id,))
class Comment(models.Model):
user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
name = models.CharField(max_length=255)
body = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s - %s' % (self.post.title, self.name)
views.py
class UpdateCommentView(UpdateView):
model = Comment
template_name = 'app1/update_comment.html'
form_class = CommentForm
def form_valid(self, form):
form.instance.post_id = self.kwargs['pk']
form.instance.user = self.request.user
return super().form_valid(form)
class AddCommentView(CreateView):
model = Comment
form_class = CommentForm
template_name = 'app1/add_comment.html'
def form_valid(self, form):
form.instance.post_id = self.kwargs['pk']
form.instance.user = self.request.user
return super().form_valid(form)
#fields = '__all__'
success_url = reverse_lazy('index')
模板
<div class="container">
<h1>Post</h1>
{% for post in object_list %}
<li><a href="{% url 'app1:article-detail' post.pk %}">{{post.title}}</a> -
{{post.author}} - <small>{{post.post_date}}</small> - <small> category : <a href="{% url 'app1:category' post.category|slugify %}">{{post.category}}</a> - </small>
{% if user.is_authenticated %}
{% if user.id == post.author.id %}
<small><a href="{% url 'app1:updatepost' post.pk %}">Edit</a></small><small>
<a href="{% url 'app1:deletepost' post.pk %}">- Delete</a>
</small></li>
{{post.snippet}}
{% endif %}
{% endif %}
您的评论模型必须是外键而不是 OneToOneField:
class Comment(models.Model):
user = models.ForeignKey(User, null = True, on_delete=models.CASCADE)
...