Django: Change TimeField to DateTimeField in models.py

后端 未结 2 415
闹比i
闹比i 2021-01-06 04:09

In my models.py, i have some models with this kind of attributes:

timestamp = models.TimeField(default=0)

I want to change them to:

相关标签:
2条回答
  • 2021-01-06 04:45

    If you didn't have any data in your tables, then you can delete your migrations from migrations folder of your app and change the field to DateTime then do the initial migration. My problem solved, without touching the SQL Queries

    0 讨论(0)
  • 2021-01-06 04:54

    That's because time cannot be converted (casted) to timestamp (neither their time-zone related variants) in PostgreSQL. F.ex. this will also fail:

    SELECT 'now'::time::timestamp
    

    In these cases, you should use the USING clause in your ALTER TABLE statement (if you can edit it directly):

    ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
    ALTER [ COLUMN ] column_name
    [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]
    

    Your query will look like, f.ex.:

    ALTER TABLE "my_model"
    ALTER COLUMN "column_name"
    SET DATA TYPE TIMESTAMP WITH TIME ZONE USING 'yesterday'::date + "column_name"
    
    0 讨论(0)
提交回复
热议问题