无法在django中保存差异日期格式 [英] Unable to save differenent date format in django

查看:258
本文介绍了无法在django中保存差异日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个表单上工作,并有一个日期字段。
我想保存日期字段的不同日期格式,而不是使用django。
我正在获得01-jan-2016的日期,并希望保存在我的数据库中。当我试图保存相同的格式时会引发错误

  [u'07 -Dec-2016'值的日期格式无效,必须为YYYY-MM-DD格式。 

我知道这种类型的问题已经问了,但是他们没有解决我的问题。 strong>



views.py

  post_date = request.POST ['date'] 

lead_obj = CustomerLeads.objects.create(posting_date = post_date)

我的models.py

  class Leads(models.Model):
customer_name = models.CharField(max_length = 50,null = True,blank = True)
title = models.CharField(max_length = 100,null = True,blank = True)
posting_date = models.DateField )


解决方案

Mysql




DATE类型用于具有日期部分但没有时间部分的值。
MySQL以YYYY-MM-DD格式检索并显示DATE值。
支持的范围为'1000-01-01'至'9999-12-31'。




Postgresql




几乎任何合理的格式接受日期和时间输入,
包括ISO 8601,SQL兼容,传统POSTGRES等。
对于某些格式,日期,月份和日期输入的订单是
含糊,并且有支持指定
这些字段的预期排序。将DateStyle参数设置为MDY以选择
月 - 日年解读,DMY选择日月 - 年
解释或YMD以选择年月日解释。


链接中的表显示postgresql接受您要查找的格式。但是如何存储?让我们试试一下插入

  INSERT INTO stackoverflow_heatwatchlist(next_date_from,next_date_to)
VALUES('1999 Jan 05','2001 6月06日)

当您选择时,您所得到的是2001-06-06和1999-01 -05'



SQlite



这里您可以插入任何您想要的格式,它将被完全保存你输入,那是因为sqlite不执行任何严格的类型检查。 sqlite中的类型纯粹是化妆品。但是django对这个问题有不同的看法,不会让你从标准格式转移太多。



SQL Server



数据的接受方式有很大的灵活性用于插入。请参阅表中的链接。但是如何存储? '1999-01-05'



总而言之



日期如何存储在表中完全是不相干的所有重要的是接受输入的格式以及它的显示方式。这就是django和python的优秀的日期和时间格式发挥作用的地方。



如果你不满意他们,你有两个选择。第一个是将日期和时间作为unix时间戳存储,并自己做所有的格式,所有你需要的是一个数学真的 - 一个相当普遍的做法。



第二个是使用CharField来存储你的数据,但这不是很有用的。您不仅必须自己做所有的格式和输入验证,任何计算都涉及到大量的字符串处理和投射。


I am working on a form and have a date field. I want to save different date format for date field instead django used. I am getting "01-jan-2016" date and want to save as it is in my database.when i am trying to save this same format is raise an error

[u"'07-Dec-2016' value has an invalid date format. It must be in YYYY-MM-DD format."].

I know this type of question asked already but they do not solve my problem.

views.py

post_date = request.POST['date']

lead_obj = CustomerLeads.objects.create(posting_date = post_date)

my models.py

class Leads(models.Model):
    customer_name = models.CharField(max_length=50,null=True, blank=True)
    title = models.CharField(max_length=100, null=True, blank=True)
    posting_date = models.DateField()

解决方案

Mysql

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

Postgresql

Date and time input is accepted in almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES, and others. For some formats, ordering of day, month, and year in date input is ambiguous and there is support for specifying the expected ordering of these fields. Set the DateStyle parameter to MDY to select month-day-year interpretation, DMY to select day-month-year interpretation, or YMD to select year-month-day interpretation.

The table at the link shows that postgresql accept the format that you are looking for. But how is it stored? Let's try an insert

INSERT INTO stackoverflow_heatwatchlist(next_date_from, next_date_to) 
VALUES('1999 Jan 05','2001 Jun 06');

And when you select, what you get is '2001-06-06' and '1999-01-05'

SQlite

Here you can insert in any format you want and it will be saved exactly as you entered, that's because sqlite does not enforce any strict type checking. Types in sqlite are purely cosmetic. but django has a different opinion on the matter and will not let you divert too much from the standard formats.

SQL Server

A lot of flexibility in how the data is accepted for insert. Refer to the table at the link. But how is it stored? '1999-01-05'

In conclusion

How the date is stored in the table is totally irrelevent, all that matters is what formats are accepted for input and how it's displayed. And that's where django's and python's excellent date and time formats come into play.

If you are not happy with them, you have two choices. The first is to store dates and times as unix timestamps and do all the formatting yourself, all you need is a little math really - a fairly common practice.

The second is to use CharField for storing your data but this is not very usefull. Not only do you have to do all the formatting and input validation yourself, any calculation would involve a lot of string processing and casting.

这篇关于无法在django中保存差异日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆