Ticket #3011: t3011_swappable_validation.diff

File t3011_swappable_validation.diff, 2.4 KB (added by mindsocket, 12 years ago)

Make model validation more robust (checks that swappable referred to in setting exists)

  • django/contrib/auth/__init__.py

    diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py
    index d5d4430..0fe0c91 100644
    a b def get_user_model():  
    9797    from django.conf import settings
    9898    from django.db.models import get_model
    9999
    100     app_label, model_name = settings.AUTH_USER_MODEL.split('.')
     100    try:
     101        app_label, model_name = settings.AUTH_USER_MODEL.split('.')
     102    except ValueError:
     103        raise ImproperlyConfigured("Invalid AUTH_USER_MODEL setting, value must be of the form 'app_label.model_name'")
    101104    return get_model(app_label, model_name)
    102105
    103106
  • django/core/management/validation.py

    diff --git a/django/core/management/validation.py b/django/core/management/validation.py
    index f613009..190fcb8 100644
    a b def get_validation_errors(outfile, app=None):  
    2323    """
    2424    from django.conf import settings
    2525    from django.db import models, connection
    26     from django.db.models.loading import get_app_errors
     26    from django.db.models.loading import get_app_errors, get_model
    2727    from django.db.models.fields.related import RelatedObject
    2828    from django.db.models.deletion import SET_NULL, SET_DEFAULT
    2929
    def get_validation_errors(outfile, app=None):  
    283283                    if r.get_accessor_name() == rel_query_name:
    284284                        e.add(opts, "Reverse query name for m2m field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
    285285
     286        # Check swappable attribute.
     287        if opts.swappable and getattr(settings, opts.swappable, None):
     288            swappable_setting = getattr(settings, opts.swappable)
     289            try:
     290                app_label, model_name = swappable_setting.split('.')
     291            except ValueError:
     292                e.add(opts, "Invalid settings.%s, '%s' not of the form 'app_label.app_name' for swapped out model '%s'." % (opts.swappable, swappable_setting, cls._meta.object_name))
     293                continue
     294            if get_model(app_label, model_name) not in models.get_models():
     295                e.add(opts, "'%s' has been swapped out in settings.%s to a model '%s.%s' that does not exist." % (cls._meta.object_name, opts.swappable, app_label, model_name))
     296
    286297        # Check ordering attribute.
    287298        if opts.ordering:
    288299            for field_name in opts.ordering:
Back to Top