Model.field 上的 If 语句

If statement on a Model.field

希望这是清楚的。

我正在尝试整理一个视图,以处理当用户对我正在尝试构建的拍卖网站上的活动列表进行出价时发生的情况。我正在做一个 if 语句来解决如果用户尝试对已关闭列表出价但 pylint 不断在读取的行上抛出语法错误时发生的情况:

如果 auction.status 不是 'active':

我试过:

如果auction.status关闭:

if auction.status.关闭:

是我遗漏的关键字还是括号?

models.py

class Listing(models.Model):

    class NewManager(models.Manager):
        def get_queryset(self):
            return super().get_queryset().filter(status='active')

    options = (
        ('active', 'Active'),
        ('closed', 'Closed'),
    )

    title = models.CharField(max_length=64)
    description = models.TextField(max_length=64)
    price = models.DecimalField(max_digits=9, decimal_places=2, validators=[MinValueValidator(0.99)])
    image = models.URLField(max_length=200, blank=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="listings")
    lister = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=None, null=True, blank=True)
    status = models.CharField(max_length=10, choices=options, default="active")
    favourites = models.ManyToManyField(User, related_name="favourite", default=None, blank=True)
    objects = models.Manager()
    listingmanager = NewManager()

    def __str__(self): 
        return f"Product: {self.title} \nDescription: {self.description} \nCurrent Price: £{self.price}\nImage: {self.image} \nCategory: {self.category} \nListed by: {self.lister}"


class Bid(models.Model):
    bidder = models.ForeignKey(User, on_delete=models.CASCADE, related_name="bidders")
    item = models.ManyToManyField(Listing, related_name="bid_item", default=None, blank=True)
    price = models.DecimalField
    time = models.TimeField()

views.py

def bidding(request, listing_id):
    bid_item = get_object_or_404(Listing, pk=listing_id)
    bid_item.resolve()
    bidding = Bid.objects.filter(bidder=request.user.filter(listing=listing).first()

    if auction.status not 'active':
        return render(request, "auctions/listingPage.html", {
            'listing': listing,
            'error': 'This auction has closed.'
        })
    
    try:
        bid_amount = request.POST['bid']
        if not bid_amout or float(bid_amount) < listing.price:
            raise(KeyError)
        if not bid:
            bid = Bid()
            bid.item = bid_item
            bid.bidder = request.user
            bid.price = bid_amount
            bid.time = timezone.now()
    except(KeyError):
        return render(request, 'auctions/listingPage.html', {
            'listing': listing,
            'error': 'Invalid bid amount.'
        })
    else:
        bid.save()
        return HttpResponseRedirect(reverse('listing', args=()))

在 python 中 is not 运算符与不等于 != 运算符不同

也在你的代码中说

if auction.status not 'active':

其中没有“是”

尝试这样做看看是否有帮助

if auction.status != 'active':