Django可复用CMS应用迁移依赖问题:兼容多版本django-cms方案咨询
Great question—this is a common pitfall when building reusable Django apps that target multiple versions of a dependency like django-cms. Let's break down the best approaches to make your migrations compatible across both versions:
Recommended: Remove All django-cms-Specific Migration Dependencies
Unless your app's migrations directly modify django-cms core models or rely on schema changes introduced in a specific cms migration (which is unlikely for most plugins), you don't need to tie your migrations to specific cms migration versions. Here's how to fix this:
Update existing migrations
- For your older 3.4.x-era migration: Replace the cms dependency with a dependency on your app's own prior migration (or leave it empty if it's your app's first migration). For example:
dependencies = [ # Replace ('cms', '0016_auto_20160608_1535'), ('your_app_name', '0001_initial'), # Use your app's actual initial migration name ] - For your new 3.5.2-era migration: Swap the cms dependency to point to the latest migration in your own app (not cms). For example:
dependencies = [ # Replace ('cms', '0020_old_tree_cleanup'), ('your_app_name', '0002_existing_plugin'), # Use your app's last migration before this one ]
- For your older 3.4.x-era migration: Replace the cms dependency with a dependency on your app's own prior migration (or leave it empty if it's your app's first migration). For example:
Why this works
- django-cms's core
CMSPluginmodel (which your plugins inherit from) has a stable schema across 3.4.x and 3.5.x. The later cms migrations like0020_old_tree_cleanupare for internal cleanup or optimizations that don't affect plugin models. - By relying only on your app's own migrations, you decouple your app from specific cms versions, making it usable across both 3.4.x and 3.5.x without migration errors.
- django-cms's core
Alternative: Conditional Dependencies (Only If Absolutely Necessary)
If your migration does depend on a schema change exclusive to django-cms 3.5.x (e.g., a new field on CMSPlugin), you'll need to handle version-specific migrations. This is more complex, but here's a rough approach:
- Create separate migration files for 3.4.x and 3.5.x environments.
- Use logic in your app's
migrations/__init__.pyto load the appropriate migration based on the installed django-cms version. - Note: This adds significant maintenance overhead, so only use this if you can't avoid relying on version-specific cms changes.
Key Testing Step
After adjusting your migrations, test them in both environments:
- Spin up a project with django-cms 3.4.x, install your app, and run
python manage.py migrate your_app_nameto confirm no errors. - Repeat the process with django-cms 3.5.2 to ensure compatibility.
内容的提问来源于stack exchange,提问作者benzkji

