Ticket #13903: patch.diff

File patch.diff, 1.7 KB (added by petraszd, 14 years ago)

bugfix and test case

  • django/db/models/base.py

     
    4343        base_meta = getattr(new_class, '_meta', None)
    4444
    4545        if getattr(meta, 'app_label', None) is None:
    46             # Figure out the app_label by looking one level up.
    47             # For 'django.contrib.sites.models', this would be 'sites'.
     46            # Figure out the app_label
     47            # For 'django.contrib.sites.models', this would be 'sites';
     48            # For 'something', this would ve 'something'.
    4849            model_module = sys.modules[new_class.__module__]
    49             kwargs = {"app_label": model_module.__name__.split('.')[-2]}
     50            namespace = model_module.__name__.split('.')
     51            if len(namespace) > 1:
     52                app_label = namespace[-2]
     53            else:
     54                app_label = namespace[0]
     55            kwargs = {"app_label": app_label}
    5056        else:
    5157            kwargs = {}
    5258
  • tests/modeltests/model_package/tests.py

     
    7777>>> Article._meta.get_field('publications').m2m_reverse_name()
    7878'publication_id'
    7979
     80# Tests app_label if model is defined in strange places
     81>>> import sys
     82>>> old_name = sys.modules[__name__].__name__
     83>>> sys.modules[__name__].__name__ = 'top_level'
     84>>> class TopLevel(models.Model): pass
     85>>> TopLevel._meta.app_label
     86'top_level'
     87
     88>>> sys.modules[__name__].__name__ = old_name
     89
    8090"""}
    8191
    8292
Back to Top