Ticket #8195: django-relative-import-app-label.patch

File django-relative-import-app-label.patch, 1.2 KB (added by kcarnold, 7 years ago)
  • django/db/models/base.py

     
    4949            meta = attr_meta
    5050        base_meta = getattr(new_class, '_meta', None)
    5151
     52        kwargs = {}
    5253        if getattr(meta, 'app_label', None) is None:
    5354            # Figure out the app_label by looking one level up.
    5455            # For 'django.contrib.sites.models', this would be 'sites'.
    5556            model_module = sys.modules[new_class.__module__]
    56             kwargs = {"app_label": model_module.__name__.split('.')[-2]}
    57         else:
    58             kwargs = {}
     57            try:
     58                app_label = model_module.__name__.split('.')[-2]
     59            except IndexError:
     60                # This was a relative import from the same directory.
     61                # Retrieve the full path.
     62                from os.path import abspath, split as path_split
     63                app_dir = path_split(abspath(model_module.__file__))[0]
     64                app_label = path_split(app_dir)[1]
     65               
     66            kwargs["app_label"] = app_label
    5967
    6068        new_class.add_to_class('_meta', Options(meta, **kwargs))
    6169        if not abstract:
Back to Top