#NEWLINE# @ cached_property #NEWLINE# def related_manager_cls( self ) : #NEWLINE# #INDENT# superclass = self . related . model . _default_manager . __class__ #NEWLINE# rel_field = self . related . field #NEWLINE# rel_model = self . related . model #NEWLINE# class RelatedManager ( superclass ) : #NEWLINE# #INDENT# def __init__( self , instance ) : #NEWLINE# #INDENT# super ( RelatedManager , self ) . __init__ ( ) #NEWLINE# self . instance = instance #NEWLINE# self . core_filters = { #NEWLINE# #INDENT# ( '%s__exact' % rel_field . name ) : instance , #NEWLINE# #UNINDENT# } #NEWLINE# self . model = rel_model #NEWLINE# #UNINDENT# def get_queryset( self ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# return self . instance . _prefetched_objects_cache [ rel_field . related_query_name ( ) ] #NEWLINE# #UNINDENT# except ( AttributeError , KeyError ) : #NEWLINE# #INDENT# db = ( self . _db or router . db_for_read ( self . model , instance = self . instance ) ) #NEWLINE# qs = super ( RelatedManager , self ) . get_queryset ( ) . using ( db ) . filter ( ** self . core_filters ) #NEWLINE# empty_strings_as_null = connections [ db ] . features . interprets_empty_strings_as_nulls #NEWLINE# for field in rel_field . foreign_related_fields : #NEWLINE# #INDENT# val = getattr ( self . instance , field . attname ) #NEWLINE# if ( ( val is None ) or ( ( val == '' ) and empty_strings_as_null ) ) : #NEWLINE# #INDENT# return qs . none ( ) #NEWLINE# #UNINDENT# #UNINDENT# qs . _known_related_objects = { #NEWLINE# #INDENT# rel_field : { #NEWLINE# #INDENT# self . instance . pk : self . instance , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# return qs #NEWLINE# #UNINDENT# #UNINDENT# def get_prefetch_queryset( self , instances ) : #NEWLINE# #INDENT# rel_obj_attr = rel_field . get_local_related_value #NEWLINE# instance_attr = rel_field . get_foreign_related_value #NEWLINE# instances_dict = dict ( ( ( instance_attr ( inst ) , inst ) for inst in instances ) ) #NEWLINE# db = ( self . _db or router . db_for_read ( self . model , instance = instances [ 0 ] ) ) #NEWLINE# query = { #NEWLINE# #INDENT# ( '%s__in' % rel_field . name ) : instances , #NEWLINE# #UNINDENT# } #NEWLINE# qs = super ( RelatedManager , self ) . get_queryset ( ) . using ( db ) . filter ( ** query ) #NEWLINE# for rel_obj in qs : #NEWLINE# #INDENT# instance = instances_dict [ rel_obj_attr ( rel_obj ) ] #NEWLINE# setattr ( rel_obj , rel_field . name , instance ) #NEWLINE# #UNINDENT# cache_name = rel_field . related_query_name ( ) #NEWLINE# return ( qs , rel_obj_attr , instance_attr , False , cache_name ) #NEWLINE# #UNINDENT# def add( self , * objs ) : #NEWLINE# #INDENT# for obj in objs : #NEWLINE# #INDENT# if ( not isinstance ( obj , self . model ) ) : #NEWLINE# #INDENT# raise TypeError ( ( "'%s' instance expected, got %r" % ( self . model . _meta . object_name , obj ) ) ) #NEWLINE# #UNINDENT# setattr ( obj , rel_field . name , self . instance ) #NEWLINE# obj . save ( ) #NEWLINE# #UNINDENT# #UNINDENT# add . alters_data = True #NEWLINE# def create( self , **kwargs ) : #NEWLINE# #INDENT# kwargs [ rel_field . name ] = self . instance #NEWLINE# db = router . db_for_write ( self . model , instance = self . instance ) #NEWLINE# return super ( RelatedManager , self . db_manager ( db ) ) . create ( ** kwargs ) #NEWLINE# #UNINDENT# create . alters_data = True #NEWLINE# def get_or_create( self , **kwargs ) : #NEWLINE# #INDENT# kwargs [ rel_field . name ] = self . instance #NEWLINE# db = router . db_for_write ( self . model , instance = self . instance ) #NEWLINE# return super ( RelatedManager , self . db_manager ( db ) ) . get_or_create ( ** kwargs ) #NEWLINE# #UNINDENT# get_or_create . alters_data = True #NEWLINE# if rel_field . null : #NEWLINE# #INDENT# def remove( self , * objs ) : #NEWLINE# #INDENT# val = rel_field . get_foreign_related_value ( self . instance ) #NEWLINE# for obj in objs : #NEWLINE# #INDENT# if ( rel_field . get_local_related_value ( obj ) == val ) : #NEWLINE# #INDENT# setattr ( obj , rel_field . name , None ) #NEWLINE# obj . save ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise rel_field . rel . to . DoesNotExist ( ( '%r is not related to %r.' % ( obj , self . instance ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# remove . alters_data = True #NEWLINE# def clear( self ) : #NEWLINE# #INDENT# self . update ( ** { #NEWLINE# #INDENT# rel_field . name : None , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# clear . alters_data = True #NEWLINE# #UNINDENT# #UNINDENT# return RelatedManager