'If statement on a Model.field
hopefully this is clear.
I am trying to put together a view that takes care of what happens when a user places a bid on an active listing on the auction site I am trying to build. I'm doing an if statement to tackle what happens if a user tries to bid on a closed listing but pylint keeps throwing a syntax error on the line that reads:
if auction.status is not 'active':
I have tried:
if auction.status is closed:
if auction.status.closed:
Is it a keyword that I'm missing or parentheses?
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=()))
Solution 1:[1]
in python the is not
operator isn't the same as the not equals !=
operator
also in your code it says
if auction.status not 'active':
which doesn't have the "is" in it
try doing this to see if it helps
if auction.status != 'active':
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |