From 3fe17d7a239f96d639f923a31c4795210b1cff4e Mon Sep 17 00:00:00 2001 From: Diederik van der Boor Date: Sun, 8 Oct 2017 19:39:51 +0200 Subject: [PATCH] Provide helpful assistence to migrate code to new 1.0+ format This makes sure the changes in 4dc20a021342b57f16884f4e951056be5cade8d8 don't cause a new range of bug reports, for things like "reverse()" no longer works. --- docs/changelog.rst | 6 ++++++ polymorphic/admin/parentadmin.py | 20 +++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 5b478ed..0c77079 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,12 @@ Changes in git -------------- * **BACKWARDS INCOMPATIBILITY:** Dropped Django 1.8 support. +* **BACKWARDS INCOMPATIBILITY:** Removed old deprecated code from 1.0, thus: + + * Import managers from ``polymorphic.managers`` (plural), not ``polymorphic.manager``. + * Register child models to the admin as well using ``@admin.register()`` or ``admin.site.register()``, + as this is no longer done automatically. + * Added ``PolymorphicTypeUndefined`` exception for incomplete imported models. When a data migration or import creates an polymorphic model, the ``polymorphic_ctype_id`` field should be filled in manually too. diff --git a/polymorphic/admin/parentadmin.py b/polymorphic/admin/parentadmin.py index 9950597..00dcb34 100644 --- a/polymorphic/admin/parentadmin.py +++ b/polymorphic/admin/parentadmin.py @@ -2,14 +2,13 @@ The parent admin displays the list view of the base model. """ import sys -import warnings -from django.conf.urls import url from django.contrib import admin from django.contrib.admin.helpers import AdminErrorList, AdminForm from django.contrib.admin.templatetags.admin_urls import add_preserved_filters from django.contrib.contenttypes.models import ContentType -from django.core.exceptions import PermissionDenied +from django.core.exceptions import PermissionDenied, ImproperlyConfigured +from django.db import models from django.http import Http404, HttpResponseRedirect from django.template.response import TemplateResponse from django.urls import RegexURLResolver @@ -39,11 +38,11 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin): A admin interface that can displays different change/delete pages, depending on the polymorphic model. To use this class, one attribute need to be defined: - * :attr:`child_models` should be a list of (Model, Admin) tuples + * :attr:`child_models` should be a list models. Alternatively, the following methods can be implemented: - * :func:`get_child_models` should return a list of (Model, ModelAdmin) tuples + * :func:`get_child_models` should return a list of models. * optionally, :func:`get_child_type_choices` can be overwritten to refine the choices for the add dialog. This class needs to be inherited by the model admin base class that is registered in the site. @@ -79,6 +78,17 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin): return self._child_models = self.get_child_models() + + # Make absolutely sure that the child models don't use the old 0.9 format, + # as of polymorphic 1.4 this deprecated configuration is no longer supported. + # Instead, register the child models in the admin too. + if self._child_models and not issubclass(self._child_models[0], models.Model): + raise ImproperlyConfigured( + "Since django-polymorphic 1.4, the `child_models` attribute " + "and `get_child_models()` method should be a list of models only.\n" + "The model-admin class should be registered in the regular Django admin." + ) + self._child_admin_site = self.admin_site self._is_setup = True