我有一个模型Order,其中创建了一个字段。目的是显示订单何时发生(日期和时间)。
我正在使用以下行在模型中创建字段:
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
但是,从管理面板中进行查看时,它显示了它是在将来创建的:27/12的晚上10:52。现在的时间是:6:11 pm。您可以在此屏幕截图中看到以下详细信息:
如何确保在productio env中记录了正确的时间?它将使用Google Cloud Products托管。
知道我在开发环境中。
创建订单的视图:
@csrf_exempt
def cart_charge(request):
culqipy.public_key = settings.CULQI_PUBLISHABLE_KEY
culqipy.secret_key = settings.CULQI_SECRET_KEY
amount = request.POST.get('amount')
currency_code = request.POST.get('currency_code')
email = request.POST.get('email')
source_id = request.POST.get('source_id')
last_four = request.POST.get('last_four')
dir_charge = {"amount": int(amount), "currency_code": currency_code,
"email": email,
"source_id": source_id}
print(dir_charge)
charge = culqipy.Charge.create(dir_charge)
transaction_amount = int(charge['amount'])/100 #Necesario dividir entre 100 para obtener el monto real,
#Esto debido a cómo Culqi recibe los datos de los pagos
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
shipping_address1 = request.user.profile.shipping_address1
shipping_address2 = request.user.profile.shipping_address2
shipping_department = request.user.profile.shipping_department
shipping_province = request.user.profile.shipping_province
shipping_district = request.user.profile.shipping_district
order = Order.objects.create(
token = charge['id'],
total =transaction_amount,
email= email, #Using email entered in Culqi module, NOT user.email. Could be diff.
last_four = last_four,
created = current_time,
shipping_address1 = shipping_address1,
shipping_address2 = shipping_address2,
shipping_department = shipping_department,
shipping_province = shipping_province,
shipping_district = shipping_district
)
order.save()
return HttpResponse("Hi")
订购型号:
class Order(models.Model):
token = models.CharField(max_length=100, blank=True, null=True)
total = models.DecimalField(max_digits=10, decimal_places=2)
email = models.EmailField(max_length=250, blank = True, verbose_name= 'Correo electrónico')
last_four = models.CharField(max_length=100, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
shipping_address1 = models.CharField(max_length=100, blank=True, null=True)
shipping_address2 = models.CharField(max_length=100, blank=True, null=True)
shipping_department = models.CharField(max_length=100, blank=True, null=True)
shipping_province = models.CharField(max_length=100, blank=True, null=True)
shipping_district = models.CharField(max_length=100, blank=True, null=True)
reason = models.CharField(max_length=400, blank=True, null=True, default='')
class Meta:
db_table = 'Order'
ordering = ['-created']
def __str__(self):
return "Order by: " + str(self.id) + " for " + str(self.total)
答案 0 :(得分:0)
创建模型对象时不需要提供当前日期,因为您已经设置了选项'auto_now_add = True',该选项将在创建模型对象时自动设置日期,例如:
order = Order.objects.create(
token = charge['id'],
total =transaction_amount,
email= email, #Using email entered in Culqi module, NOT user.email. Could be diff.
last_four = last_four,
shipping_address1 = shipping_address1,
shipping_address2 = shipping_address2,
shipping_department = shipping_department,
shipping_province = shipping_province,
shipping_district = shipping_district
)
order.save()
print(order.created) # return current time on in server's timezone