Django

Code

Ticket #7154: 0001-Now-all-managers-not-only-the-default-manager-gets.patch

File 0001-Now-all-managers-not-only-the-default-manager-gets.patch, 4.0 kB (added by sebastian_noack, 7 months ago)
  • a/django/db/models/base.py

    old new  
    1515from django.db import connection, transaction 
    1616from django.db.models import signals 
    1717from django.db.models.loading import register_models, get_model 
     18from django.db.models.manager import Manager 
    1819from django.dispatch import dispatcher 
    1920from django.utils.datastructures import SortedDict 
    2021from django.utils.functional import curry 
     
    6566                if not hasattr(meta, 'get_latest_by'): 
    6667                    new_class._meta.get_latest_by = base_meta.get_latest_by 
    6768 
    68         old_default_mgr = None 
    69         if getattr(new_class, '_default_manager', None): 
    70             # We have a parent who set the default manager. 
    71             if new_class._default_manager.model._meta.abstract: 
    72                 old_default_mgr = new_class._default_manager 
    73             new_class._default_manager = None 
    7469        if getattr(new_class._meta, 'app_label', None) is None: 
    7570            # Figure out the app_label by looking one level up. 
    7671            # For 'django.contrib.sites.models', this would be 'sites'. 
     
    106101                    new_class.add_to_class(attr_name, field) 
    107102                new_class._meta.parents[base] = field 
    108103            else: 
    109                 # The abstract base class case
     104                # Copy fields from the abstract base class
    110105                names = set([f.name for f in new_class._meta.local_fields + new_class._meta.many_to_many]) 
    111106                for field in base._meta.local_fields + base._meta.local_many_to_many: 
    112107                    if field.name in names: 
     
    114109                                % (field.name, name, base.__name__)) 
    115110                    new_class.add_to_class(field.name, copy.deepcopy(field)) 
    116111 
     112                # Copy managers from the abstract base class. 
     113                for attr, value in ((attr, getattr(base, attr)) for attr in dir(base)): 
     114                    if value == getattr(new_class, attr) and isinstance(value, Manager): 
     115                        new_class.add_to_class(attr, copy.copy(value)) 
     116 
    117117        if abstract: 
    118118            # Abstract base models can't be instantiated and don't appear in 
    119119            # the list of models for an app. We do the final setup for them a 
     
    122122            new_class.Meta = attr_meta 
    123123            return new_class 
    124124 
    125         if old_default_mgr and not new_class._default_manager: 
    126             new_class._default_manager = old_default_mgr._copy_to_model(new_class) 
    127125        new_class._prepare() 
    128126        register_models(new_class._meta.app_label, new_class) 
    129127 
  • a/django/db/models/manager.py

    old new  
    3636        if not getattr(model, '_default_manager', None) or self.creation_counter < model._default_manager.creation_counter: 
    3737            model._default_manager = self 
    3838 
    39     def _copy_to_model(self, model): 
    40         """ 
    41         Makes a copy of the manager and assigns it to 'model', which should be 
    42         a child of the existing model (used when inheriting a manager from an 
    43         abstract base class). 
    44         """ 
    45         assert issubclass(model, self.model) 
    46         mgr = copy.copy(self) 
    47         mgr.model = model 
    48         return mgr 
    49  
    5039    ####################### 
    5140    # PROXIES TO QUERYSET # 
    5241    #######################