在Django中将DateField更改为DateTimeField [英] Changing DateField to DateTimeField in Django

查看:587
本文介绍了在Django中将DateField更改为DateTimeField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前的应用程序模型文件中,我有一些发布日期字段,开始和结束日期。但是我想将它们从DateFields更改为DateTimeFields。这将为用户设置发布日期提供一点灵活性,因为他们想指定一个时间。

In my current models file for my app I've got some publish date fields, start and end date. However I'd like to change them from DateFields to DateTimeFields. This will give the user a little more flexibility when setting a publish date, as they would like to specify a time.

我正在使用南方来进行数据库迁移,更改数据库中的字段时会有什么问题吗?还是需要删除它?然后将其添加为DateTimeField?

I'm using south to do the database migrations, will I have any problems when changing the field in the database? Or will need to remove it? Then add it as a DateTimeField?

一旦这样做,我可以设置一个默认的时间吗?例如,我希望开始日期从早上9点开始。结束日期在午夜完成。

Once I've done that, will i be able to set a default time? for example I'd like the start date to start at 9 in the morning. and the end date to finish at midnight.

无论如何,这里是我想改变的模型。

Anyway here are the models i'd like to change.

start_date = models.DateField()
end_date = models.DateField()

我想更改为:

start_date = models.DateTimeField()
end_date = models.DateTimeField()

任何帮助将不胜感激。

推荐答案

当您将 DateField 更改为 DateTimeField south将只是改变它,所以它不会冲突。

It turns out when you change a DateField to a DateTimeField south will just change it for you so it won't conflict.

例如我跑了:

python manage.py schemamigration --auto hotel

并得到这个输出:

 ~ Changed field start_date on hotel.Event
 ~ Changed field end_date on hotel.Event
Created 0018_auto__chg_field_event_start_date__chg_field_event_end_date.py. You can now apply this migration with: ./manage.py migrate hotel

尽管我会提供备份数据库进行任何更改。

Although i would advide backing up the database before making any changes.

这篇关于在Django中将DateField更改为DateTimeField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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