new to dj from flask.
working on a school project creating an e-bay like auction site.
I’m trying to implement an auto bid feature , but having trouble with the auto bid function.
the issue seems to be that the competing Bid Model objects are failing to retain the edits to their amount attribute as they climb their way up the bid increment to their respective max bid amounts.
I made a simple test file in python containing the required class attributes, and the logic is sound, I’m starting to suspect that the django model object save mechanism doesn’t work the way I think it works.
also, even though the return of the Highlander function…(there can be only One…high bid)
may contain the wrong values, the connection to the Auction object as the foreign key .high_bid is not made, which is also confusing.
I was under the impression that Python passes objects by reference.
Suggestions would be greatly appreciated
Models.py
class Auction(models.Model):
...#stuff
high_bid=models.ForeignKey('Bid', on_delete=models.CASCADE, blank=True, null=True, default=None, related_name="highest_bid")
bid_step=models.DecimalField(null=False, max_digits=5, decimal_places=2, default=1.00, )
#more stuff...
class Bid(models.Model):
# foreign key references omitted here
amount=models.DecimalField(null=False, max_digits=10, decimal_places=2, )
auto_bid=models.BooleanField(null=False, default=False, )
max_amount=models.DecimalField(null=False,max_digits=10, decimal_places=2, default=0.00, )
Views.py
bid object created and form validated with .is_valid()
if not form.full_clean():
model.save()
result=Auction_Validate_Bid(model)
if result['valid']:
auct=Auction.objects.get(pk=request.POST['auction'])
auct.high_bid=result['high_bid']
auct.save(update_fields=['high_bid'])
return HttpResponseRedirect(reverse('listing')+'?q='+ request.POST['listing'],{
"auctions": get_data_from_auction(Auction.objects.get(pk=request.POST['auction'])), "watching": watching, "list_title": "Auction Listing" + request.POST['listing'], "icons": icons, "form": form
})
bid values validator
def Auction_Validate_Bid(new_bid):
#validate bid
this_auction=Auction.objects.get(id=new_bid.auction.id)
#check amount and max amount are %0 of step
if not new_bid.amount % this_auction.bid_step:
if new_bid.auto_bid==True and not new_bid.max_amount % this_auction.bid_step:
print("bid has valid values...comparing bids...")
#get other bids, if none exist new_bid is the high_bid
auctionBid=Bid.objects.all().filter(auction=new_bid.auction).order_by("amount").first()
if auctionBid:
if new_bid.amount > auctionBid.amount:
print("new bid is higher than existing bid...there can be only One high bid!")
lambert=highlander(auctionBid, new_bid, this_auction.bid_step)
print("highlander returned: ", lambert)
return {"valid": True, "high_bid": lambert}
return {"valid": False, "error": "amount or max amount not '% 0' of bid_step"}
auto bid function.py
def highlander(auctionBid, new_bid, step):
if new_bid.auto_bid or auctionBid.auto_bid and (new_bid.ammount != new_bid.max_ammount or auctionBid.ammount != auctionBid.max_ammount):
if auctionBid.auto_bid:
auctionBid.amount=auctionBid.amount + step
print("iterating auctionBid to: ", auctionBid.amount)
auctionBid.save(update_fields=['amount'])
if auctionBid.amount==auctionBid.max_amount:
print("auctionBid is capped")
auctionBid.auto_bid=False
auctionBid.save(update_fields=['auto_bid'])
if new_bid.amount < auctionBid.amount and new_bid.auto_bid and new_bid.amount < new_bid.max_amount:
new_bid.amount= new_bid.amount + step
print("iterating new_bid to: ", new_bid.amount)
new_bid.save(update_fields=['ammount'])
if new_bid.amount == new_bid.max_amount:
new_bid.auto_bid=False
new_bid.save(update_fields=['auto_bid'])
if new_bid.amount == auctionBid.amount:
print("re-entering function")
highlander(auctionBid, new_bid, step)
else:
print("Outcome: ")
print("\tauctionBid", auctionBid)
print("\tnew_bid", new_bid)
if new_bid.amount > auctionBid.amount:
return new_bid
else:
return auctionBid
return new_bid