'Creates the managers used by other methods (__get__() and delete()).'

def create_manager(self, instance, superclass):

    rel_field = self.related.field
    rel_model = self.related.model
    class RelatedManager(superclass, ):
        def get_query_set(self):
            db = (self._db or router.db_for_read(rel_model, instance=instance))
            return superclass.get_query_set(self).using(db).filter(**self.core_filters)
        def add(self, *objs):
            for obj in objs:
                if (not isinstance(obj, self.model)):
                    raise TypeError(("'%s'    instance    expected" % self.model._meta.object_name))
                setattr(obj, rel_field.name, instance)
                obj.save()
        add.alters_data = True
        def create(self, **kwargs):
            kwargs.update({rel_field.name: instance})
            db = router.db_for_write(rel_model, instance=instance)
            return super(RelatedManager, self.db_manager(db)).create(**kwargs)
        create.alters_data = True
        def get_or_create(self, **kwargs):
            kwargs.update({rel_field.name: instance})
            db = router.db_for_write(rel_model, instance=instance)
            return super(RelatedManager, self.db_manager(db)).get_or_create(**kwargs)
        get_or_create.alters_data = True
        if rel_field.null:
            def remove(self, *objs):
                val = getattr(instance, rel_field.rel.get_related_field().attname)
                for obj in objs:
                    if (getattr(obj, rel_field.attname) == val):
                        setattr(obj, rel_field.name, None)
                        obj.save()
                    else:
                        raise rel_field.rel.to.DoesNotExist(('%r    is    not    related    to    %r.' % (obj, instance)))
            remove.alters_data = True
            def clear(self):
                for obj in self.all():
                    setattr(obj, rel_field.name, None)
                    obj.save()
            clear.alters_data = True
    manager = RelatedManager()
    attname = rel_field.rel.get_related_field().name
    manager.core_filters = {('%s__%s' % (rel_field.name, attname)): getattr(instance, attname)}
    manager.model = self.related.model
    return manager
