@cached_property
    def related_manager_cls(self):
        # Dynamically create a class that subclasses the related model's default
        # manager.
        superclass = self.related.model._default_manager.__class__
        rel_field = self.related.field
        rel_model = self.related.model

        class RelatedManager(superclass):
            def __init__(self, instance):
                super(RelatedManager, self).__init__()
                self.instance = instance
                self.core_filters= {'%s__exact' % rel_field.name: instance}
                self.model = rel_model

            def get_queryset(self):
                try:
                    return self.instance._prefetched_objects_cache[rel_field.related_query_name()]
                except (AttributeError, __HOLE__):
                    db = self._db or router.db_for_read(self.model, instance=self.instance)
                    qs = super(RelatedManager, self).get_queryset().using(db).filter(**self.core_filters)
                    empty_strings_as_null = connections[db].features.interprets_empty_strings_as_nulls
                    for field in rel_field.foreign_related_fields:
                        val = getattr(self.instance, field.attname)
                        if val is None or (val == '' and empty_strings_as_null):
                            return qs.none()
                    qs._known_related_objects = {rel_field: {self.instance.pk: self.instance}}
                    return qs

            def get_prefetch_queryset(self, instances):
                rel_obj_attr = rel_field.get_local_related_value
                instance_attr = rel_field.get_foreign_related_value
                instances_dict = dict((instance_attr(inst), inst) for inst in instances)
                db = self._db or router.db_for_read(self.model, instance=instances[0])
                query = {'%s__in' % rel_field.name: instances}
                qs = super(RelatedManager, self).get_queryset().using(db).filter(**query)
                # Since we just bypassed this class' get_queryset(), we must manage
                # the reverse relation manually.
                for rel_obj in qs:
                    instance = instances_dict[rel_obj_attr(rel_obj)]
                    setattr(rel_obj, rel_field.name, instance)
                cache_name = rel_field.related_query_name()
                return qs, rel_obj_attr, instance_attr, False, cache_name

            def add(self, *objs):
                for obj in objs:
                    if not isinstance(obj, self.model):
                        raise TypeError("'%s' instance expected, got %r" % (self.model._meta.object_name, obj))
                    setattr(obj, rel_field.name, self.instance)
                    obj.save()
            add.alters_data = True

            def create(self, **kwargs):
                kwargs[rel_field.name] = self.instance
                db = router.db_for_write(self.model, instance=self.instance)
                return super(RelatedManager, self.db_manager(db)).create(**kwargs)
            create.alters_data = True

            def get_or_create(self, **kwargs):
                # Update kwargs with the related object that this
                # ForeignRelatedObjectsDescriptor knows about.
                kwargs[rel_field.name] = self.instance
                db = router.db_for_write(self.model, instance=self.instance)
                return super(RelatedManager, self.db_manager(db)).get_or_create(**kwargs)
            get_or_create.alters_data = True

            # remove() and clear() are only provided if the ForeignKey can have a value of null.
            if rel_field.null:
                def remove(self, *objs):
                    val = rel_field.get_foreign_related_value(self.instance)
                    for obj in objs:
                        # Is obj actually part of this descriptor set?
                        if rel_field.get_local_related_value(obj) == val:
                            setattr(obj, rel_field.name, None)
                            obj.save()
                        else:
                            raise rel_field.rel.to.DoesNotExist("%r is not related to %r." % (obj, self.instance))
                remove.alters_data = True

                def clear(self):
                    self.update(**{rel_field.name: None})
                clear.alters_data = True

        return RelatedManager
dataset/ETHPy150Open cloudera/hue/desktop/core/ext-py/Django-1.6.10/django/db/models/fields/related.py/ForeignRelatedObjectsDescriptor.related_manager_cls