繁体   English   中英

我应该使用哪个字段来允许多个评论?

[英]which field should i use to allow more than one comment?

我正在尝试向拍卖页面添加评论,但只允许在每次拍卖时添加评论,我应该使用哪个模型字段来允许多个评论?

当我尝试在管理页面中添加多个评论时,页面显示它:

此拍卖的评论已经存在。

模型.py:

class Auction_listings(models.Model):
    product_image = models.CharField(max_length=500)
    product_title = models.CharField(max_length=40)
    product_description = models.CharField(max_length=200)
    product_category = models.CharField(max_length=20, default="others")
    product_price = models.FloatField() #IntegerField()
    username = models.CharField(max_length=64)
    post_date = models.DateField(auto_now_add=True)

    def __str__(self):
        return f"{self.product_title}"


class Bids(models.Model):
    auction = models.OneToOneField(Auction_listings, primary_key=True, on_delete=models.CASCADE)
    username = models.CharField(max_length=64)
    is_closed = models.BooleanField(default=False)
    bid_value = models.FloatField()
    
    def __str__(self):
        return f"by {self.username} in {self.auction}: {self.bid_value}"


class Comments(models.Model):
    auction = models.OneToOneField(Auction_listings, primary_key=True, on_delete=models.CASCADE)
    username = models.CharField(max_length=64)
    content = models.CharField(max_length=150)
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return f"by {self.username} in {self.auction} on {self.date}"

谢谢你先进

当您使用 OneToOne 字段时,您会将一条评论连接到一次拍卖(一对一)。 对于每次拍卖的多个评论,您应该使用如下外键字段:

class Comments(models.Model):
    auction = models.ForeignKey(Auction_listings, on_delete=models.CASCADE)
    username = models.CharField(max_length=64)
    content = models.CharField(max_length=150)
    date = models.DateField(auto_now_add=True)

def __str__(self):
    return f"by {self.username} in {self.auction} on {self.date}"

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM