def ensure_default_manager(sender, **kwargs):
 cls = sender
 if cls._meta.abstract:
  return
 if (not getattr(cls, '_default_manager', None)):
  try:
   cls._meta.get_field('objects')
   raise ValueError(("Model %s must specify a custom Manager, because it has a field named 'objects'" % cls.__name__))
  except FieldDoesNotExist:
   pass
  cls.add_to_class('objects', Manager())
  cls._base_manager = cls.objects
 elif (not getattr(cls, '_base_manager', None)):
  default_mgr = cls._default_manager.__class__
  if ((default_mgr is Manager) or getattr(default_mgr, 'use_for_related_fields', False)):
   cls._base_manager = cls._default_manager
  else:
   for base_class in default_mgr.mro()[1:]:
    if ((base_class is Manager) or getattr(base_class, 'use_for_related_fields', False)):
     cls.add_to_class('_base_manager', base_class())
     return
   raise AssertionError('Should never get here. Please report a bug, including your model and model manager setup.')

def ensure_default_manager ( sender , ** kwargs ) : cls = sender if cls . _meta . abstract : return if ( not getattr ( cls , '_default_manager' , None ) ) : try : cls . _meta . get_field ( 'objects' ) raise ValueError ( ( "Model %s must specify a custom Manager, because it has a field named 'objects'" % cls . __name__ ) ) except FieldDoesNotExist : pass cls . add_to_class ( 'objects' , Manager ( ) ) cls . _base_manager = cls . objects elif ( not getattr ( cls , '_base_manager' , None ) ) : default_mgr = cls . _default_manager . __class__ if ( ( default_mgr is Manager ) or getattr ( default_mgr , 'use_for_related_fields' , False ) ) : cls . _base_manager = cls . _default_manager else : for base_class in default_mgr . mro ( ) [ 1 : ] : if ( ( base_class is Manager ) or getattr ( base_class , 'use_for_related_fields' , False ) ) : cls . add_to_class ( '_base_manager' , base_class ( ) ) return raise AssertionError ( 'Should never get here. Please report a bug, including your model and model manager setup.' )
ensures that a model subclass contains a default manager and sets the _default_manager attribute on the class .