You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Django的租车Web应用执行migrate时出现datetime类型错误求助

Fixing Django Migration Error: TypeError: fromisoformat: argument must be str After Removing DateTime Fields

Let's break down why you're hitting this error and how to fix it quickly:

What's Causing the Issue?

Even though you removed datetime-related code from your models.py, the old migration files still contain references to those fields with invalid configurations. Specifically, look at your first migration file:

migrations.AddField(
    model_name='cart',
    name='renting_date',
    field=models.DateTimeField(default=1),
    preserve_default=False,
),

You set default=1 for a DateTimeField—Django tries to parse this integer as an ISO datetime string, which fails with the fromisoformat: argument must be str error you're seeing. The migration system doesn't care that you removed the field from your model later; it tries to run all existing migrations in order.

Step-by-Step Fixes

Option 1: Reset Migrations (Best for Development, No Critical Data)

If you're still in development and don't have important data in your database, this is the quickest way:

  • Delete all files in your app/migrations folder except __init__.py.
  • Delete your database file (e.g., db.sqlite3 for SQLite).
  • Regenerate fresh initial migrations:
    python manage.py makemigrations
    
  • Apply the new migrations:
    python manage.py migrate
    

Option 2: Fix Existing Migrations (For When You Can't Lose Data)

If you need to keep your database data, edit the problematic migration files:

  1. Open the first migration file with the renting_date field. Either:
    • Replace default=1 with a valid datetime default (like default=django.utils.timezone.now—don't forget to add import django.utils.timezone at the top of the migration file), or
    • Delete the entire AddField operation for renting_date (since you removed this field from your model anyway).
  2. Remove any other migration operations that reference fields you've deleted (like the second migration altering renting_date to DateField, and the third adding ordered_date to OrderPlaced if that field is gone from your model).
  3. Run the migration again:
    python manage.py migrate
    

Verify the Fix

After applying either option, run:

python manage.py showmigrations

You should see all migrations marked as applied, and running your app shouldn't show any "pending migrations" warnings anymore.

Pro Tip for Future Development

Django migrations are meant to be immutable once applied (especially in production). If you experiment with fields and decide to remove them, either:

  • Delete the unapplied migration files immediately before running migrate, or
  • Generate a reverse migration to remove the field properly with python manage.py makemigrations --empty app then writing the removal logic.

内容的提问来源于stack exchange,提问作者Rushikesh Lakhotiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:47:30