设置managed=False后Django仍生成迁移文件问题求助
Hey there, let's figure out why your managed=False model is still showing up in migrations even though the Django docs say it shouldn't. Here are the most common reasons and fixes:
1. Double-check your Meta class setup
First, make sure your managed=False is correctly placed in the model's inner Meta class with proper Python indentation. Even a tiny indentation mistake can make Django ignore this setting. Your code snippet looks correct, but it's worth verifying in your actual project files—sometimes copy-paste errors or typos sneak in (like misspelling managed as manage).
2. Manually remove the model from the generated migration file
Since you're seeing Create model FieldOpsBooking in your initial migration, you can edit the generated 0001_initial.py file and delete the entire CreateModel entry for FieldOpsBooking.
Django's managed=False flag tells it not to handle table creation/deletion for that model, so this entry shouldn't exist in the migration. After removing it, run python manage.py migrate and Django will skip attempting to create that table.
3. Check for cross-model dependencies
If another model (like RewardManagementLeads in your output) has a foreign key or many-to-many relationship pointing to FieldOpsBooking, Django might include references to it in migrations. However, it still shouldn't generate a CreateModel command for the managed=False model—if it does, that's likely a quirk in initial migration generation.
In this case, keep valid foreign key constraints if they're needed (just ensure the actual field_ops_booking table exists in your database already) but still delete the CreateModel line for FieldOpsBooking.
4. Verify your Django version
You referenced Django 3.1 docs—confirm your project is actually running this version by running:
python -m django --version
Older Django versions might have slightly different behavior with managed=False in initial migrations.
5. Reset migrations if issues persist
If you're still stuck, you can reset migrations for the affected apps:
- Delete all migration files in
user_analysis/migrations/andod_engagement/migrations/(keep the__init__.pyfile). - Clear migration records from the database:
DELETE FROM django_migrations WHERE app IN ('user_analysis', 'od_engagement'); - Re-run
python manage.py makemigrations—double-check yourmanaged=Falsesetting is correct before doing this.
Once you fix the migration file, Django will respect the managed=False setting going forward and won't generate future migrations for that model.
内容的提问来源于stack exchange,提问作者Akshay Hazari

