<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import warnings <EOL> from operator import attrgetter <EOL> from django . db import connections , router , transaction <EOL> from django . db . models import Q , signals <EOL> from django . db . models . query import QuerySet <EOL> from django . utils . deprecation import RemovedInDjango20Warning <EOL> from django . utils . functional import cached_property <EOL> class ForwardManyToOneDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field_with_rel ) : <EOL> self . field = field_with_rel <EOL> self . cache_name = self . field . get_cache_name ( ) <EOL> @ cached_property <EOL> def RelatedObjectDoesNotExist ( self ) : <EOL> return type ( <EOL> str ( '<STR_LIT>' ) , <EOL> ( self . field . remote_field . model . DoesNotExist , AttributeError ) , <EOL> { } <EOL> ) <EOL> def is_cached ( self , instance ) : <EOL> return hasattr ( instance , self . cache_name ) <EOL> def get_queryset ( self , ** hints ) : <EOL> manager = self . field . remote_field . model . _default_manager <EOL> if not getattr ( manager , '<STR_LIT>' , False ) : <EOL> manager = self . field . remote_field . model . _base_manager <EOL> return manager . db_manager ( hints = hints ) . all ( ) <EOL> def get_prefetch_queryset ( self , instances , queryset = None ) : <EOL> if queryset is None : <EOL> queryset = self . get_queryset ( ) <EOL> queryset . _add_hints ( instance = instances [ <NUM_LIT:0> ] ) <EOL> rel_obj_attr = self . field . get_foreign_related_value <EOL> instance_attr = self . field . get_local_related_value <EOL> instances_dict = { instance_attr ( inst ) : inst for inst in instances } <EOL> related_field = self . field . foreign_related_fields [ <NUM_LIT:0> ] <EOL> if self . field . remote_field . is_hidden ( ) or len ( self . field . foreign_related_fields ) == <NUM_LIT:1> : <EOL> query = { '<STR_LIT>' % related_field . name : set ( instance_attr ( inst ) [ <NUM_LIT:0> ] for inst in instances ) } <EOL> else : <EOL> query = { '<STR_LIT>' % self . field . related_query_name ( ) : instances } <EOL> queryset = queryset . filter ( ** query ) <EOL> if not self . field . remote_field . multiple : <EOL> rel_obj_cache_name = self . field . remote_field . get_cache_name ( ) <EOL> for rel_obj in queryset : <EOL> instance = instances_dict [ rel_obj_attr ( rel_obj ) ] <EOL> setattr ( rel_obj , rel_obj_cache_name , instance ) <EOL> return queryset , rel_obj_attr , instance_attr , True , self . cache_name <EOL> def __get__ ( self , instance , cls = None ) : <EOL> """<STR_LIT>""" <EOL> if instance is None : <EOL> return self <EOL> try : <EOL> rel_obj = getattr ( instance , self . cache_name ) <EOL> except AttributeError : <EOL> val = self . field . get_local_related_value ( instance ) <EOL> if None in val : <EOL> rel_obj = None <EOL> else : <EOL> qs = self . get_queryset ( instance = instance ) <EOL> qs = qs . filter ( self . field . get_reverse_related_filter ( instance ) ) <EOL> rel_obj = qs . get ( ) <EOL> if not self . field . remote_field . multiple : <EOL> setattr ( rel_obj , self . field . remote_field . get_cache_name ( ) , instance ) <EOL> setattr ( instance , self . cache_name , rel_obj ) <EOL> if rel_obj is None and not self . field . null : <EOL> raise self . RelatedObjectDoesNotExist ( <EOL> "<STR_LIT>" % ( self . field . model . __name__ , self . field . name ) <EOL> ) <EOL> else : <EOL> return rel_obj <EOL> def __set__ ( self , instance , value ) : <EOL> """<STR_LIT>""" <EOL> if value is not None and not isinstance ( value , self . field . remote_field . model . _meta . concrete_model ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( <EOL> value , <EOL> instance . _meta . object_name , <EOL> self . field . name , <EOL> self . field . remote_field . model . _meta . object_name , <EOL> ) <EOL> ) <EOL> elif value is not None : <EOL> if instance . _state . db is None : <EOL> instance . _state . db = router . db_for_write ( instance . __class__ , instance = value ) <EOL> elif value . _state . db is None : <EOL> value . _state . db = router . db_for_write ( value . __class__ , instance = instance ) <EOL> elif value . _state . db is not None and instance . _state . db is not None : <EOL> if not router . allow_relation ( value , instance ) : <EOL> raise ValueError ( '<STR_LIT>' % value ) <EOL> if value is None : <EOL> related = getattr ( instance , self . cache_name , None ) <EOL> if related is not None : <EOL> setattr ( related , self . field . remote_field . get_cache_name ( ) , None ) <EOL> for lh_field , rh_field in self . field . related_fields : <EOL> setattr ( instance , lh_field . attname , None ) <EOL> else : <EOL> for lh_field , rh_field in self . field . related_fields : <EOL> setattr ( instance , lh_field . attname , getattr ( value , rh_field . attname ) ) <EOL> setattr ( instance , self . cache_name , value ) <EOL> if value is not None and not self . field . remote_field . multiple : <EOL> setattr ( value , self . field . remote_field . get_cache_name ( ) , instance ) <EOL> class ReverseOneToOneDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , related ) : <EOL> self . related = related <EOL> self . cache_name = related . get_cache_name ( ) <EOL> @ cached_property <EOL> def RelatedObjectDoesNotExist ( self ) : <EOL> return type ( <EOL> str ( '<STR_LIT>' ) , <EOL> ( self . related . related_model . DoesNotExist , AttributeError ) , <EOL> { } <EOL> ) <EOL> def is_cached ( self , instance ) : <EOL> return hasattr ( instance , self . cache_name ) <EOL> def get_queryset ( self , ** hints ) : <EOL> manager = self . related . related_model . _default_manager <EOL> if not getattr ( manager , '<STR_LIT>' , False ) : <EOL> manager = self . related . related_model . _base_manager <EOL> return manager . db_manager ( hints = hints ) . all ( ) <EOL> def get_prefetch_queryset ( self , instances , queryset = None ) : <EOL> if queryset is None : <EOL> queryset = self . get_queryset ( ) <EOL> queryset . _add_hints ( instance = instances [ <NUM_LIT:0> ] ) <EOL> rel_obj_attr = attrgetter ( self . related . field . attname ) <EOL> def instance_attr ( obj ) : <EOL> return obj . _get_pk_val ( ) <EOL> instances_dict = { instance_attr ( inst ) : inst for inst in instances } <EOL> query = { '<STR_LIT>' % self . related . field . name : instances } <EOL> queryset = queryset . filter ( ** query ) <EOL> rel_obj_cache_name = self . related . field . get_cache_name ( ) <EOL> for rel_obj in queryset : <EOL> instance = instances_dict [ rel_obj_attr ( rel_obj ) ] <EOL> setattr ( rel_obj , rel_obj_cache_name , instance ) <EOL> return queryset , rel_obj_attr , instance_attr , True , self . cache_name <EOL> def __get__ ( self , instance , cls = None ) : <EOL> """<STR_LIT>""" <EOL> if instance is None : <EOL> return self <EOL> try : <EOL> rel_obj = getattr ( instance , self . cache_name ) <EOL> except AttributeError : <EOL> related_pk = instance . _get_pk_val ( ) <EOL> if related_pk is None : <EOL> rel_obj = None <EOL> else : <EOL> filter_args = self . related . field . get_forward_related_filter ( instance ) <EOL> try : <EOL> rel_obj = self . get_queryset ( instance = instance ) . get ( ** filter_args ) <EOL> except self . related . related_model . DoesNotExist : <EOL> rel_obj = None <EOL> else : <EOL> setattr ( rel_obj , self . related . field . get_cache_name ( ) , instance ) <EOL> setattr ( instance , self . cache_name , rel_obj ) <EOL> if rel_obj is None : <EOL> raise self . RelatedObjectDoesNotExist ( <EOL> "<STR_LIT>" % ( <EOL> instance . __class__ . __name__ , <EOL> self . related . get_accessor_name ( ) <EOL> ) <EOL> ) <EOL> else : <EOL> return rel_obj <EOL> def __set__ ( self , instance , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> try : <EOL> rel_obj = getattr ( instance , self . cache_name ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> delattr ( instance , self . cache_name ) <EOL> setattr ( rel_obj , self . related . field . name , None ) <EOL> elif not isinstance ( value , self . related . related_model ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( <EOL> value , <EOL> instance . _meta . object_name , <EOL> self . related . get_accessor_name ( ) , <EOL> self . related . related_model . _meta . object_name , <EOL> ) <EOL> ) <EOL> else : <EOL> if instance . _state . db is None : <EOL> instance . _state . db = router . db_for_write ( instance . __class__ , instance = value ) <EOL> elif value . _state . db is None : <EOL> value . _state . db = router . db_for_write ( value . __class__ , instance = instance ) <EOL> elif value . _state . db is not None and instance . _state . db is not None : <EOL> if not router . allow_relation ( value , instance ) : <EOL> raise ValueError ( '<STR_LIT>' % value ) <EOL> related_pk = tuple ( getattr ( instance , field . attname ) for field in self . related . field . foreign_related_fields ) <EOL> for index , field in enumerate ( self . related . field . local_related_fields ) : <EOL> setattr ( value , field . attname , related_pk [ index ] ) <EOL> setattr ( instance , self . cache_name , value ) <EOL> setattr ( value , self . related . field . get_cache_name ( ) , instance ) <EOL> class ReverseManyToOneDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rel ) : <EOL> self . rel = rel <EOL> self . field = rel . field <EOL> @ cached_property <EOL> def related_manager_cls ( self ) : <EOL> return create_reverse_many_to_one_manager ( <EOL> self . rel . related_model . _default_manager . __class__ , <EOL> self . rel , <EOL> ) <EOL> def __get__ ( self , instance , cls = None ) : <EOL> """<STR_LIT>""" <EOL> if instance is None : <EOL> return self <EOL> return self . related_manager_cls ( instance ) <EOL> def __set__ ( self , instance , value ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % self . rel . get_accessor_name ( ) , RemovedInDjango20Warning , stacklevel = <NUM_LIT:2> , <EOL> ) <EOL> manager = self . __get__ ( instance ) <EOL> manager . set ( value ) <EOL> def create_reverse_many_to_one_manager ( superclass , rel ) : <EOL> """<STR_LIT>""" <EOL> class RelatedManager ( superclass ) : <EOL> def __init__ ( self , instance ) : <EOL> super ( RelatedManager , self ) . __init__ ( ) <EOL> self . instance = instance <EOL> self . model = rel . related_model <EOL> self . field = rel . field <EOL> self . core_filters = { self . field . name : instance } <EOL> def __call__ ( self , ** kwargs ) : <EOL> manager = getattr ( self . model , kwargs . pop ( '<STR_LIT>' ) ) <EOL> manager_class = create_reverse_many_to_one_manager ( manager . __class__ , rel ) <EOL> return manager_class ( self . instance ) <EOL> do_not_call_in_templates = True <EOL> def _apply_rel_filters ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> db = self . _db or router . db_for_read ( self . model , instance = self . instance ) <EOL> empty_strings_as_null = connections [ db ] . features . interprets_empty_strings_as_nulls <EOL> queryset . _add_hints ( instance = self . instance ) <EOL> if self . _db : <EOL> queryset = queryset . using ( self . _db ) <EOL> queryset = queryset . filter ( ** self . core_filters ) <EOL> for field in self . field . foreign_related_fields : <EOL> val = getattr ( self . instance , field . attname ) <EOL> if val is None or ( val == '<STR_LIT>' and empty_strings_as_null ) : <EOL> return queryset . none ( ) <EOL> queryset . _known_related_objects = { self . field : { self . instance . pk : self . instance } } <EOL> return queryset <EOL> def get_queryset ( self ) : <EOL> try : <EOL> return self . instance . _prefetched_objects_cache [ self . field . related_query_name ( ) ] <EOL> except ( AttributeError , KeyError ) : <EOL> queryset = super ( RelatedManager , self ) . get_queryset ( ) <EOL> return self . _apply_rel_filters ( queryset ) <EOL> def get_prefetch_queryset ( self , instances , queryset = None ) : <EOL> if queryset is None : <EOL> queryset = super ( RelatedManager , self ) . get_queryset ( ) <EOL> queryset . _add_hints ( instance = instances [ <NUM_LIT:0> ] ) <EOL> queryset = queryset . using ( queryset . _db or self . _db ) <EOL> rel_obj_attr = self . field . get_local_related_value <EOL> instance_attr = self . field . get_foreign_related_value <EOL> instances_dict = { instance_attr ( inst ) : inst for inst in instances } <EOL> query = { '<STR_LIT>' % self . field . name : instances } <EOL> queryset = queryset . filter ( ** query ) <EOL> for rel_obj in queryset : <EOL> instance = instances_dict [ rel_obj_attr ( rel_obj ) ] <EOL> setattr ( rel_obj , self . field . name , instance ) <EOL> cache_name = self . field . related_query_name ( ) <EOL> return queryset , rel_obj_attr , instance_attr , False , cache_name <EOL> def add ( self , * objs , ** kwargs ) : <EOL> bulk = kwargs . pop ( '<STR_LIT>' , True ) <EOL> objs = list ( objs ) <EOL> db = router . db_for_write ( self . model , instance = self . instance ) <EOL> def check_and_update_obj ( obj ) : <EOL> if not isinstance ( obj , self . model ) : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> self . model . _meta . object_name , obj , <EOL> ) ) <EOL> setattr ( obj , self . field . name , self . instance ) <EOL> if bulk : <EOL> pks = [ ] <EOL> for obj in objs : <EOL> check_and_update_obj ( obj ) <EOL> if obj . _state . adding or obj . _state . db != db : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % obj <EOL> ) <EOL> pks . append ( obj . pk ) <EOL> self . model . _base_manager . using ( db ) . filter ( pk__in = pks ) . update ( ** { <EOL> self . field . name : self . instance , <EOL> } ) <EOL> else : <EOL> with transaction . atomic ( using = db , savepoint = False ) : <EOL> for obj in objs : <EOL> check_and_update_obj ( obj ) <EOL> obj . save ( ) <EOL> add . alters_data = True <EOL> def create ( self , ** kwargs ) : <EOL> kwargs [ self . field . name ] = self . instance <EOL> db = router . db_for_write ( self . model , instance = self . instance ) <EOL> return super ( RelatedManager , self . db_manager ( db ) ) . create ( ** kwargs ) <EOL> create . alters_data = True <EOL> def get_or_create ( self , ** kwargs ) : <EOL> kwargs [ self . field . name ] = self . instance <EOL> db = router . db_for_write ( self . model , instance = self . instance ) <EOL> return super ( RelatedManager , self . db_manager ( db ) ) . get_or_create ( ** kwargs ) <EOL> get_or_create . alters_data = True <EOL> def update_or_create ( self , ** kwargs ) : <EOL> kwargs [ self . field . name ] = self . instance <EOL> db = router . db_for_write ( self . model , instance = self . instance ) <EOL> return super ( RelatedManager , self . db_manager ( db ) ) . update_or_create ( ** kwargs ) <EOL> update_or_create . alters_data = True <EOL> if rel . field . null : <EOL> def remove ( self , * objs , ** kwargs ) : <EOL> if not objs : <EOL> return <EOL> bulk = kwargs . pop ( '<STR_LIT>' , True ) <EOL> val = self . field . get_foreign_related_value ( self . instance ) <EOL> old_ids = set ( ) <EOL> for obj in objs : <EOL> if self . field . get_local_related_value ( obj ) == val : <EOL> old_ids . add ( obj . pk ) <EOL> else : <EOL> raise self . field . remote_field . model . DoesNotExist ( <EOL> "<STR_LIT>" % ( obj , self . instance ) <EOL> ) <EOL> self . _clear ( self . filter ( pk__in = old_ids ) , bulk ) <EOL> remove . alters_data = True <EOL> def clear ( self , ** kwargs ) : <EOL> bulk = kwargs . pop ( '<STR_LIT>' , True ) <EOL> self . _clear ( self , bulk ) <EOL> clear . alters_data = True <EOL> def _clear ( self , queryset , bulk ) : <EOL> db = router . db_for_write ( self . model , instance = self . instance ) <EOL> queryset = queryset . using ( db ) <EOL> if bulk : <EOL> queryset . update ( ** { self . field . name : None } ) <EOL> else : <EOL> with transaction . atomic ( using = db , savepoint = False ) : <EOL> for obj in queryset : <EOL> setattr ( obj , self . field . name , None ) <EOL> obj . save ( update_fields = [ self . field . name ] ) <EOL> _clear . alters_data = True <EOL> def set ( self , objs , ** kwargs ) : <EOL> objs = tuple ( objs ) <EOL> bulk = kwargs . pop ( '<STR_LIT>' , True ) <EOL> clear = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if self . field . null : <EOL> db = router . db_for_write ( self . model , instance = self . instance ) <EOL> with transaction . atomic ( using = db , savepoint = False ) : <EOL> if clear : <EOL> self . clear ( ) <EOL> self . add ( * objs , bulk = bulk ) <EOL> else : <EOL> old_objs = set ( self . using ( db ) . all ( ) ) <EOL> new_objs = [ ] <EOL> for obj in objs : <EOL> if obj in old_objs : <EOL> old_objs . remove ( obj ) <EOL> else : <EOL> new_objs . append ( obj ) <EOL> self . remove ( * old_objs , bulk = bulk ) <EOL> self . add ( * new_objs , bulk = bulk ) <EOL> else : <EOL> self . add ( * objs , bulk = bulk ) <EOL> set . alters_data = True <EOL> return RelatedManager <EOL> class ManyToManyDescriptor ( ReverseManyToOneDescriptor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rel , reverse = False ) : <EOL> super ( ManyToManyDescriptor , self ) . __init__ ( rel ) <EOL> self . reverse = reverse <EOL> @ property <EOL> def through ( self ) : <EOL> return self . rel . through <EOL> @ cached_property <EOL> def related_manager_cls ( self ) : <EOL> model = self . rel . related_model if self . reverse else self . rel . model <EOL> return create_forward_many_to_many_manager ( <EOL> model . _default_manager . __class__ , <EOL> self . rel , <EOL> reverse = self . reverse , <EOL> ) <EOL> def create_forward_many_to_many_manager ( superclass , rel , reverse ) : <EOL> """<STR_LIT>""" <EOL> class ManyRelatedManager ( superclass ) : <EOL> def __init__ ( self , instance = None ) : <EOL> super ( ManyRelatedManager , self ) . __init__ ( ) <EOL> self . instance = instance <EOL> if not reverse : <EOL> self . model = rel . model <EOL> self . query_field_name = rel . field . related_query_name ( ) <EOL> self . prefetch_cache_name = rel . field . name <EOL> self . source_field_name = rel . field . m2m_field_name ( ) <EOL> self . target_field_name = rel . field . m2m_reverse_field_name ( ) <EOL> self . symmetrical = rel . symmetrical <EOL> else : <EOL> self . model = rel . related_model <EOL> self . query_field_name = rel . field . name <EOL> self . prefetch_cache_name = rel . field . related_query_name ( ) <EOL> self . source_field_name = rel . field . m2m_reverse_field_name ( ) <EOL> self . target_field_name = rel . field . m2m_field_name ( ) <EOL> self . symmetrical = False <EOL> self . through = rel . through <EOL> self . reverse = reverse <EOL> self . source_field = self . through . _meta . get_field ( self . source_field_name ) <EOL> self . target_field = self . through . _meta . get_field ( self . target_field_name ) <EOL> self . core_filters = { } <EOL> for lh_field , rh_field in self . source_field . related_fields : <EOL> core_filter_key = '<STR_LIT>' % ( self . query_field_name , rh_field . name ) <EOL> self . core_filters [ core_filter_key ] = getattr ( instance , rh_field . attname ) <EOL> self . related_val = self . source_field . get_foreign_related_value ( instance ) <EOL> if None in self . related_val : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( instance , self . source_field_name ) ) <EOL> if instance . pk is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> instance . __class__ . __name__ ) <EOL> def __call__ ( self , ** kwargs ) : <EOL> manager = getattr ( self . model , kwargs . pop ( '<STR_LIT>' ) ) <EOL> manager_class = create_forward_many_to_many_manager ( manager . __class__ , rel , reverse ) <EOL> return manager_class ( instance = self . instance ) <EOL> do_not_call_in_templates = True <EOL> def _build_remove_filters ( self , removed_vals ) : <EOL> filters = Q ( ** { self . source_field_name : self . related_val } ) <EOL> removed_vals_filters = ( not isinstance ( removed_vals , QuerySet ) or <EOL> removed_vals . _has_filters ( ) ) <EOL> if removed_vals_filters : <EOL> filters &= Q ( ** { '<STR_LIT>' % self . target_field_name : removed_vals } ) <EOL> if self . symmetrical : <EOL> symmetrical_filters = Q ( ** { self . target_field_name : self . related_val } ) <EOL> if removed_vals_filters : <EOL> symmetrical_filters &= Q ( <EOL> ** { '<STR_LIT>' % self . source_field_name : removed_vals } ) <EOL> filters |= symmetrical_filters <EOL> return filters <EOL> def _apply_rel_filters ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> queryset . _add_hints ( instance = self . instance ) <EOL> if self . _db : <EOL> queryset = queryset . using ( self . _db ) <EOL> return queryset . _next_is_sticky ( ) . filter ( ** self . core_filters ) <EOL> def get_queryset ( self ) : <EOL> try : <EOL> return self . instance . _prefetched_objects_cache [ self . prefetch_cache_name ] <EOL> except ( AttributeError , KeyError ) : <EOL> queryset = super ( ManyRelatedManager , self ) . get_queryset ( ) <EOL> return self . _apply_rel_filters ( queryset ) <EOL> def get_prefetch_queryset ( self , instances , queryset = None ) : <EOL> if queryset is None : <EOL> queryset = super ( ManyRelatedManager , self ) . get_queryset ( ) <EOL> queryset . _add_hints ( instance = instances [ <NUM_LIT:0> ] ) <EOL> queryset = queryset . using ( queryset . _db or self . _db ) <EOL> query = { '<STR_LIT>' % self . query_field_name : instances } <EOL> queryset = queryset . _next_is_sticky ( ) . filter ( ** query ) <EOL> fk = self . through . _meta . get_field ( self . source_field_name ) <EOL> join_table = self . through . _meta . db_table <EOL> connection = connections [ queryset . db ] <EOL> qn = connection . ops . quote_name <EOL> queryset = queryset . extra ( select = { <EOL> '<STR_LIT>' % f . attname : <EOL> '<STR_LIT>' % ( qn ( join_table ) , qn ( f . column ) ) for f in fk . local_related_fields } ) <EOL> return ( <EOL> queryset , <EOL> lambda result : tuple ( <EOL> getattr ( result , '<STR_LIT>' % f . attname ) <EOL> for f in fk . local_related_fields <EOL> ) , <EOL> lambda inst : tuple ( <EOL> f . get_db_prep_value ( getattr ( inst , f . attname ) , connection ) <EOL> for f in fk . foreign_related_fields <EOL> ) , <EOL> False , <EOL> self . prefetch_cache_name , <EOL> ) <EOL> def add ( self , * objs ) : <EOL> if not rel . through . _meta . auto_created : <EOL> opts = self . through . _meta <EOL> raise AttributeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( opts . app_label , opts . object_name ) <EOL> ) <EOL> db = router . db_for_write ( self . through , instance = self . instance ) <EOL> with transaction . atomic ( using = db , savepoint = False ) : <EOL> self . _add_items ( self . source_field_name , self . target_field_name , * objs ) <EOL> if self . symmetrical : <EOL> self . _add_items ( self . target_field_name , self . source_field_name , * objs ) <EOL> add . alters_data = True <EOL> def remove ( self , * objs ) : <EOL> if not rel . through . _meta . auto_created : <EOL> opts = self . through . _meta <EOL> raise AttributeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( opts . app_label , opts . object_name ) <EOL> ) <EOL> self . _remove_items ( self . source_field_name , self . target_field_name , * objs ) <EOL> remove . alters_data = True <EOL> def clear ( self ) : <EOL> db = router . db_for_write ( self . through , instance = self . instance ) <EOL> with transaction . atomic ( using = db , savepoint = False ) : <EOL> signals . m2m_changed . send ( <EOL> sender = self . through , action = "<STR_LIT>" , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = None , using = db , <EOL> ) <EOL> filters = self . _build_remove_filters ( super ( ManyRelatedManager , self ) . get_queryset ( ) . using ( db ) ) <EOL> self . through . _default_manager . using ( db ) . filter ( filters ) . delete ( ) <EOL> signals . m2m_changed . send ( <EOL> sender = self . through , action = "<STR_LIT>" , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = None , using = db , <EOL> ) <EOL> clear . alters_data = True <EOL> def set ( self , objs , ** kwargs ) : <EOL> if not rel . through . _meta . auto_created : <EOL> opts = self . through . _meta <EOL> raise AttributeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( opts . app_label , opts . object_name ) <EOL> ) <EOL> objs = tuple ( objs ) <EOL> clear = kwargs . pop ( '<STR_LIT>' , False ) <EOL> db = router . db_for_write ( self . through , instance = self . instance ) <EOL> with transaction . atomic ( using = db , savepoint = False ) : <EOL> if clear : <EOL> self . clear ( ) <EOL> self . add ( * objs ) <EOL> else : <EOL> old_ids = set ( self . using ( db ) . values_list ( self . target_field . target_field . attname , flat = True ) ) <EOL> new_objs = [ ] <EOL> for obj in objs : <EOL> fk_val = ( <EOL> self . target_field . get_foreign_related_value ( obj ) [ <NUM_LIT:0> ] <EOL> if isinstance ( obj , self . model ) else obj <EOL> ) <EOL> if fk_val in old_ids : <EOL> old_ids . remove ( fk_val ) <EOL> else : <EOL> new_objs . append ( obj ) <EOL> self . remove ( * old_ids ) <EOL> self . add ( * new_objs ) <EOL> set . alters_data = True <EOL> def create ( self , ** kwargs ) : <EOL> if not self . through . _meta . auto_created : <EOL> opts = self . through . _meta <EOL> raise AttributeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( opts . app_label , opts . object_name ) <EOL> ) <EOL> db = router . db_for_write ( self . instance . __class__ , instance = self . instance ) <EOL> new_obj = super ( ManyRelatedManager , self . db_manager ( db ) ) . create ( ** kwargs ) <EOL> self . add ( new_obj ) <EOL> return new_obj <EOL> create . alters_data = True <EOL> def get_or_create ( self , ** kwargs ) : <EOL> db = router . db_for_write ( self . instance . __class__ , instance = self . instance ) <EOL> obj , created = super ( ManyRelatedManager , self . db_manager ( db ) ) . get_or_create ( ** kwargs ) <EOL> if created : <EOL> self . add ( obj ) <EOL> return obj , created <EOL> get_or_create . alters_data = True <EOL> def update_or_create ( self , ** kwargs ) : <EOL> db = router . db_for_write ( self . instance . __class__ , instance = self . instance ) <EOL> obj , created = super ( ManyRelatedManager , self . db_manager ( db ) ) . update_or_create ( ** kwargs ) <EOL> if created : <EOL> self . add ( obj ) <EOL> return obj , created <EOL> update_or_create . alters_data = True <EOL> def _add_items ( self , source_field_name , target_field_name , * objs ) : <EOL> from django . db . models import Model <EOL> if objs : <EOL> new_ids = set ( ) <EOL> for obj in objs : <EOL> if isinstance ( obj , self . model ) : <EOL> if not router . allow_relation ( obj , self . instance ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % <EOL> ( obj , self . instance . _state . db , obj . _state . db ) <EOL> ) <EOL> fk_val = self . through . _meta . get_field ( <EOL> target_field_name ) . get_foreign_related_value ( obj ) [ <NUM_LIT:0> ] <EOL> if fk_val is None : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % <EOL> ( obj , target_field_name ) <EOL> ) <EOL> new_ids . add ( fk_val ) <EOL> elif isinstance ( obj , Model ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % <EOL> ( self . model . _meta . object_name , obj ) <EOL> ) <EOL> else : <EOL> new_ids . add ( obj ) <EOL> db = router . db_for_write ( self . through , instance = self . instance ) <EOL> vals = ( self . through . _default_manager . using ( db ) <EOL> . values_list ( target_field_name , flat = True ) <EOL> . filter ( ** { <EOL> source_field_name : self . related_val [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' % target_field_name : new_ids , <EOL> } ) ) <EOL> new_ids = new_ids - set ( vals ) <EOL> with transaction . atomic ( using = db , savepoint = False ) : <EOL> if self . reverse or source_field_name == self . source_field_name : <EOL> signals . m2m_changed . send ( <EOL> sender = self . through , action = '<STR_LIT>' , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = new_ids , using = db , <EOL> ) <EOL> self . through . _default_manager . using ( db ) . bulk_create ( [ <EOL> self . through ( ** { <EOL> '<STR_LIT>' % source_field_name : self . related_val [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' % target_field_name : obj_id , <EOL> } ) <EOL> for obj_id in new_ids <EOL> ] ) <EOL> if self . reverse or source_field_name == self . source_field_name : <EOL> signals . m2m_changed . send ( <EOL> sender = self . through , action = '<STR_LIT>' , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = new_ids , using = db , <EOL> ) <EOL> def _remove_items ( self , source_field_name , target_field_name , * objs ) : <EOL> if not objs : <EOL> return <EOL> old_ids = set ( ) <EOL> for obj in objs : <EOL> if isinstance ( obj , self . model ) : <EOL> fk_val = self . target_field . get_foreign_related_value ( obj ) [ <NUM_LIT:0> ] <EOL> old_ids . add ( fk_val ) <EOL> else : <EOL> old_ids . add ( obj ) <EOL> db = router . db_for_write ( self . through , instance = self . instance ) <EOL> with transaction . atomic ( using = db , savepoint = False ) : <EOL> signals . m2m_changed . send ( <EOL> sender = self . through , action = "<STR_LIT>" , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = old_ids , using = db , <EOL> ) <EOL> target_model_qs = super ( ManyRelatedManager , self ) . get_queryset ( ) <EOL> if target_model_qs . _has_filters ( ) : <EOL> old_vals = target_model_qs . using ( db ) . filter ( ** { <EOL> '<STR_LIT>' % self . target_field . target_field . attname : old_ids } ) <EOL> else : <EOL> old_vals = old_ids <EOL> filters = self . _build_remove_filters ( old_vals ) <EOL> self . through . _default_manager . using ( db ) . filter ( filters ) . delete ( ) <EOL> signals . m2m_changed . send ( <EOL> sender = self . through , action = "<STR_LIT>" , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = old_ids , using = db , <EOL> ) <EOL> return ManyRelatedManager </s>
