[英]When creating a database model in Django, how can I create fields that will only accept a month and year, respectively?
If I use something like 如果我使用类似
year_published = models.DateField()
then I have to enter in a day and month, in addition to a year. 然后除了一年之外,我还必须输入一天和一个月。 But in this field I only want to enter a year.
但是在这个领域我只想输入一年。 Likewise, I only want to enter a month in
同样,我只想输入一个月
month_published = models.DateField()
Any idea how to do this? 任何想法如何做到这一点?
If you only store the year, how will you sort on Month and Date? 如果仅存储年份,那么如何对月份和日期进行排序? Instead, store the full datetime object or split out the full date into three fields.
而是,存储完整的datetime对象或将完整的日期分为三个字段。 As already mentioned, these would normally be Char fields, but can be Int and can be converted back to date to sort and compare/diff.
如前所述,这些通常是Char字段,但可以是Int字段,可以转换回最新日期以进行排序和比较/比较。 See:
看到:
How to convert integer into date object python? 如何将整数转换为日期对象python?
Or simply store the full date natively and then get the chunks you need from it as you need it. 或者直接在本地存储完整日期,然后根据需要从中获取所需的数据块。 You might check out the Pendulum Package which opens up a lot more options on date operations.
您可能会签出Pendulum软件包 ,该软件包在日期操作上打开了更多选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.