Ticket #11936: fix_r11689.diff

File fix_r11689.diff, 2.4 KB (added by clamothe, 6 years ago)

Fix and regression test against r11689

  • django/db/models/loading.py

    diff -r cf42f6bf945b django/db/models/loading.py
    a b  
    1212__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models',
    1313        'load_app', 'app_cache_ready')
    1414
     15def filter_deferred_models(models):
     16    """
     17    Takes a list of models and returns the models that are not deferred.
     18   
     19    Deferred models are temporary models that are created to represent the
     20    results of queries that are made using the ``only`` and ``defer``
     21    ``QuerySet`` methods.
     22    """
     23    return [model for model in models if not model._deferred]
     24
    1525class AppCache(object):
    1626    """
    1727    A cache that stores installed applications and their models. Used to
     
    135145        """
    136146        Given a module containing models, returns a list of the models.
    137147        Otherwise returns a list of all installed models.
     148       
     149        Temporary models that were created for representing the results of
     150        deferred-field querysets are not included.
    138151        """
    139152        self._populate()
    140153        if app_mod:
    141             return self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict()).values()
     154            model_list = self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict()).values()
    142155        else:
    143156            model_list = []
    144157            for app_entry in self.app_models.itervalues():
    145158                model_list.extend(app_entry.values())
    146             return model_list
     159       
     160        return filter_deferred_models(model_list)
    147161
    148162    def get_model(self, app_label, model_name, seed_cache=True):
    149163        """
  • tests/modeltests/defer/models.py

    diff -r cf42f6bf945b tests/modeltests/defer/models.py
    a b  
    183183>>> obj.name = "bb"
    184184>>> obj.save()
    185185
     186# Regression for #11936 - loading.get_models does not return deferred models
     187>>> from django.db.models.loading import get_models
     188>>> get_models(models.get_app('defer'))
     189[<class 'modeltests.defer.models.Secondary'>, <class 'modeltests.defer.models.Primary'>, <class 'modeltests.defer.models.Child'>, <class 'modeltests.defer.models.BigChild'>]
     190
    186191# Finally, we need to flush the app cache for the defer module.
    187192# Using only/defer creates some artifical entries in the app cache
    188193# that messes up later tests. Purge all entries, just to be sure.
Back to Top