升级至Django 1.11.12时models/manager.py中contribute_to_class报错求助
Hey there! Upgrading a neglected Django project to 1.11.12 after a long break is definitely tricky—dependency mismatches and deprecated model features are super common culprits here. Let’s walk through the most likely fixes to get your project back up and running:
1. First, Capture the Exact Error Traceback
Before diving into fixes, make sure you copy the full error traceback from your runserver output. The traceback will point directly to which model, field, or dependency is failing (e.g., a deprecated field type, incompatible easy-thumbnails method, or psycopg2 database layer mismatch). Without this specific info we’re guessing, but here are the most common issues tied to your setup:
2. Fix easy-thumbnails Compatibility with Django 1.11
easy-thumbnails has strict version requirements for different Django releases. The latest versions target newer Django versions, so you need to lock in a release explicitly tested for 1.11. Run this command to install a compatible version:
pip install easy-thumbnails==2.4.1
Also, double-check your models.py for thumbnail-related fields:
- If you use
ThumbnailerImageField, ensure you’re not using deprecated arguments (like oldupload_tobehaviors that were removed in Django 1.11). - Verify your
THUMBNAIL_SETTINGSinsettings.pydon’t reference retired Django features (e.g., outdated storage backends).
3. Validate psycopg2 Version for Django 1.11
psycopg2 2.7.x is the stable, compatible version for Django 1.11. Newer releases (2.8+) can clash with Django’s older database layer. Install a working version with:
pip install psycopg2==2.7.7
Also confirm your DATABASES setting uses the correct engine: django.db.backends.postgresql_psycopg2—Django 1.11 still supports this, so don’t switch to the simplified engine name used in newer versions yet.
4. Check for Deprecated Model Features in Django 1.11
Django 1.11 removed or deprecated several model-related features that might be in your old code:
- Implicit reverse relation
related_namedefaults: If you haveForeignKeyfields without an explicitrelated_name, Django 1.11 may throw naming conflict errors. Addrelated_nameto all foreign keys to resolve this. - Retired
Model._meta.get_all_field_names(): Replace this withModel._meta.get_fields()orModel._meta.get_field_names()depending on your use case. - Removed
SubfieldBasefor custom fields: If you have custom fields usingdjango.db.models.fields.subclassing.SubfieldBase, switch to inheriting directly fromdjango.db.models.Fieldand implementto_python()andget_prep_value()instead. - Updated
ManyToManyFieldthrough model rules: Ensure anythroughmodels for many-to-many relations have proper foreign keys to both linked models, per Django 1.11’s requirements.
5. Run Django’s Built-in Upgrade Checks
Django has tools to flag compatibility issues automatically. Run this command to get a tailored list of deprecations and errors in your project:
python manage.py check --deploy
This will highlight model problems, misconfigured settings, and other gaps that could be causing your startup error.
6. Clear Cached Files to Rule Out Conflicts
Old cached thumbnails or compiled Python files can cause unexpected issues. Clear these to eliminate caching as a factor:
- Delete all files in your project’s
media/thumbnailsdirectory (if using easy-thumbnails) - Run
python manage.py clearcache - Delete all
__pycache__folders in your app directories
If you can share the exact traceback from your error, we can narrow this down even further—but these steps should cover the most common pain points when upgrading a neglected project to Django 1.11.12.
内容的提问来源于stack exchange,提问作者Codejoy

