I have a problem with my auction site because when I publish an auction the function doesn't save my variable correctly, these are my codes:
views.py
import datetime
from datetime import timedelta
def publishAuction(request):
user=request.user
form = auctionForm(request.POST, request.FILES)
if request.method=="POST":
if form.is_valid():
auction=form.save(commit=False)
auction.advertiser=user
auction.startDate=datetime.datetime.now()
auction.endDate=auction.startDate+timedelta(days=1)
auction.endPrice=request.POST.get("startPrice")
auction.save()
return redirect("../")
else:
form=auctionForm()
models.py
class Auction(models.Model):
advertiser = models.ForeignKey(User, on_delete=models.CASCADE, related_name='auction_advertiser', null=True)
winner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='auction_winner', null=True)
title = models.CharField(max_length=30)
text = models.TextField()
startPrice = models.FloatField(null=True)
endPrice = models.FloatField()
status = models.BooleanField(default=True)
image = models.ImageField(upload_to='images/')
startDate = models.DateTimeField(auto_now=False, auto_now_add=True)
endDate = models.DateTimeField(auto_now=False, auto_now_add=True)
The publication is done correctly, everything except endDate that is always equal to startDate...
I have tried also with datetime.datetime.now instead of auction.startDate or datetime.timedelta(days=1) but when I get the value in the shell is always the same...
In the scratch.py file I write the same codes and it works, I don't know why doesn't work in the function... :S
Thanks to everyone!
question from:
https://stackoverflow.com/questions/65833653/django-python-problems-with-timedelta-saving-form 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…