[英]cs50w commerce project : IntegrityError UNIQUE constraint failed
我正在处理 cs50w 的商业项目,试图创建一个新列表,但我不断收到此错误:
IntegrityError at /new
UNIQUE constraint failed: auctions_listings.user_id
这是我的用户和列表模型:
class User(AbstractUser):
pass
#def __str__(self):
#return f"{self.username}"
class Listings(models.Model):
title = models.CharField(max_length=30)
desc = models.TextField()
#starting_bid = models.FloatField(validators = [MinValueValidator(1)])
img = models.URLField(blank=True)
category = models.CharField(blank=True,max_length=20)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="owner", primary_key=True)
这是我对新上市的看法:
def new(request):
if request.method == "POST":
if not request.user.is_authenticated:
return render(request, "auctions/new_listing.html", {
"message": "Try logging in first"
})
user = request.user
######################################################
for u in User.objects.all():
print(f" UID = = {u.pk} | Name = = {u}")
######################################################
title = request.POST["title"]
img = request.POST["image"]
category = request.POST["category"]
#starting_bid = request.POST["starting_bid"]
description = request.POST["description"]
listing = Listings.objects.create(
user = user,
title = title,
img = img,
category = category,
#starting_bid = starting_bid,
desc = description
)
if listing is not None:
listing.save()
return redirect("index")
return render(request, "auctions/new_listing.html")
以及创建新列表的表格:
<form action="{% url 'new' %}" method="post" class="form-horizontal" style="width: 50%; margin: auto;">
{% csrf_token %}
<div class="form-group">
<label for="title">Title: </label>
<input type="text" name="title" id="title" class="form-control" autofocus required>
</div>
<div class="form-group">
<label for="image">Image URL: </label>
<input type="url" name="image" class="form-control" id="image" autofocus>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Category: </label>
<select class="form-control" id="exampleFormControlSelect1" name="category">
<option>clothing</option>
<option>electronics</option>
<option>collectables</option>
<option>appliances</option>
<option>furniture</option>
<option>other</option>
</select>
</div>
<!--
<div class="form-group">
<label for="starting_bid">Starting Bid: </label>
<input type="number" id="starting_bid" name="starting_bid" class="form-control" autofocus>
</div>
-->
<div class="form-group">
<label for="description">Description: </label>
<textarea class="form-control" id="description" name="description" rows="6" autofocus required>
</textarea>
</div>
<div class="form-group">
<input type="submit">
</div>
</form>
我在所有提到的地方都注释掉了起拍价,因为我也遇到了这个问题,但我现在并不真正担心这个,现在我只想能够创建一两个列表。
我已经运行python manage.py makemigrations
和python manage.py migrate
。
这个错误发生在我的 4 个用户中,每个用户都有一个自动递增的 ID。
我可以打印出用户和他们的 ID 就好了。
我也收到此警告:
auctions.Listings.user: (fields.W342) Setting unique=True on
a ForeignKey has the same effect as using a OneToOneField.
HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
但是在使用一对一字段或多对一字段时,我仍然会遇到同样的错误。 最终我希望每个用户都有很多列表,但现在,我只是想至少成功创建一个列表。
这不是必需的:
if not request.user.is_authenticated:
return render(request, "auctions/new_listing.html", {
"message": "Try logging in first"
})
这也可能是问题所在。 尝试这个:
@login_required
编辑:我认为这是问题所在。 您已将外键设置为主键。 您应该删除“primary_key=True”,django 将自行创建一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.