我试图建立一个电子商务网站(CS50项目2),以节省评论。这些注释以前是保存的,但随后我将ForeignKeys添加到我的评论模型中,将其链接到清单和用户模型。现在,每当我试图保存注释时,就会发生此错误。
IntegrityError at /listing/1/
NOT NULL constraint failed: auctions_comments.user_id
Request Method: POST
Request URL: http://127.0.0.1:8000/listing/1/
Django Version: 3.2.5
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: auctions_comments.user_id这一行代码被高亮显示为comment.save()。
models.py:
class User(AbstractUser):
pass
class Listings(models.Model):
CATEGORY = [
("Miscellaneous", "Miscellaneous"),
("Movies and Television", "Movies and Television"),
("Sports", "Sports"),
("Arts and Crafts", "Arts and Crafts"),
("Clothing", "Clothing"),
("Books", "Books"),
]
title = models.CharField(max_length=64)
description = models.CharField(max_length=500)
bid = models.DecimalField(max_digits=1000000000000, decimal_places=2)
image = models.URLField(null=True, blank=True)
category = models.CharField(max_length=64, choices=CATEGORY, default=None)
class Comments(models.Model):
listing = models.ForeignKey(Listings, on_delete=models.CASCADE, default="")
user = models.ForeignKey(User, on_delete=models.CASCADE, default="")
comment = models.CharField(max_length=500)views.py:
@login_required(login_url='login')
def listing(request, id):
listing = Listings.objects.get(id=id)
comment_obj = Comments.objects.filter(listing=listing)
form = CommentForm()
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.listing = listing
comment.save()
else:
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj
})
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj
})html or template file:
{% block body %}
<img src ="{{ auction_listing.image }}" style = "height: 10%; width: 10%;">
<h4 class = "text">{{ auction_listing.title }}</h4>
<h6>Description: {{ auction_listing.description }}</h6>
<h6>Category: {{ auction_listing.category }}</h6>
<h6>Price: ${{ auction_listing.bid }}</h6>
<form action = "{% url 'listing' auction_listing.id %}" method = "POST">
{% csrf_token %}
{{ form }}
<input type = "submit" value = "Save">
</form>
{% for comment in comments %}
<h6> {{ comment.comment }} </h6>
{% endfor %}
<button type = "button">Add to Watchlist</button>
{% endblock %}我认为问题在于views.py中的views.py()和html中的表单,其中写着"{% url‘列表’auction_listing.id %}",但我不知道如何修复它。
forms.py:
class ListingsForm(forms.ModelForm):
class Meta:
model = Listings
fields = ['title', 'description', 'bid', 'image', 'category']
class CommentForm(forms.ModelForm):
class Meta:
model = Comments
fields = ['comment']发布于 2022-05-22 20:53:45
我假设用户用来发布评论的表单没有让他们指定他们的listing ID或user ID。如果是这样的话,那么Comments中的listing和user都是空的,这就是导致错误的原因,因为外键不允许为null。您必须在视图中手动填写丢失的数据。看起来您已经计算出了清单值,并将其添加到表单实例中。你只需要为用户做同样的事情。request参数有一个user字段,您可以使用该字段来计算user ID。
if form.is_valid():
comment = form.save(commit=False)
comment.listing = listing
comment.user = request.user
comment.save()https://stackoverflow.com/questions/72341163
复制相似问题