(Django - Python) timedelta 保存形式的问题
(Django - Python) Problems with timedelta saving form
我的拍卖网站有问题,因为当我发布拍卖时,函数没有正确保存我的变量,这些是我的代码:
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)
发布已正确完成,除 endDate 之外的所有内容始终等于 startDate...
我也尝试过使用 datetime.datetime.now 而不是 auction.startDate 或 datetime.timedelta(days=1) 但是当我得到 shell 中的值时总是一样的...
在 scratch.py 文件中我写了相同的代码并且它有效,我不知道为什么在函数中不起作用... :S
谢谢大家!
您已在 endDate
字段上定义了 auto_now_add=True
,这使得字段不可编辑并设置为 now()。来自 documentation:
Automatically set the field to now when the object is first created.
Useful for creation of timestamps. Note that the current date is
always used; it’s not just a default value that you can override. So
even if you set a value for this field when creating the object, it
will be ignored.
我的拍卖网站有问题,因为当我发布拍卖时,函数没有正确保存我的变量,这些是我的代码:
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)
发布已正确完成,除 endDate 之外的所有内容始终等于 startDate...
我也尝试过使用 datetime.datetime.now 而不是 auction.startDate 或 datetime.timedelta(days=1) 但是当我得到 shell 中的值时总是一样的...
在 scratch.py 文件中我写了相同的代码并且它有效,我不知道为什么在函数中不起作用... :S
谢谢大家!
您已在 endDate
字段上定义了 auto_now_add=True
,这使得字段不可编辑并设置为 now()。来自 documentation:
Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override. So even if you set a value for this field when creating the object, it will be ignored.