<s> from django . conf import settings <EOL> from django . db import connection , router , transaction <EOL> from django . db . backends import util <EOL> from django . db . models import signals , get_model <EOL> from django . db . models . fields import ( AutoField , Field , IntegerField , <EOL> PositiveIntegerField , PositiveSmallIntegerField , FieldDoesNotExist ) <EOL> from django . db . models . related import RelatedObject <EOL> from django . db . models . query import QuerySet <EOL> from django . db . models . query_utils import QueryWrapper <EOL> from django . db . models . deletion import CASCADE <EOL> from django . utils . encoding import smart_unicode <EOL> from django . utils . translation import ( ugettext_lazy as _ , string_concat , <EOL> ungettext , ugettext ) <EOL> from django . utils . functional import curry <EOL> from django . core import exceptions <EOL> from django import forms <EOL> RECURSIVE_RELATIONSHIP_CONSTANT = '<STR_LIT>' <EOL> pending_lookups = { } <EOL> def add_lazy_relation ( cls , field , relation , operation ) : <EOL> """<STR_LIT>""" <EOL> if relation == RECURSIVE_RELATIONSHIP_CONSTANT : <EOL> app_label = cls . _meta . app_label <EOL> model_name = cls . __name__ <EOL> else : <EOL> try : <EOL> app_label , model_name = relation . split ( "<STR_LIT:.>" ) <EOL> except ValueError : <EOL> app_label = cls . _meta . app_label <EOL> model_name = relation <EOL> except AttributeError : <EOL> app_label = relation . _meta . app_label <EOL> model_name = relation . _meta . object_name <EOL> model = get_model ( app_label , model_name , False ) <EOL> if model : <EOL> operation ( field , model , cls ) <EOL> else : <EOL> key = ( app_label , model_name ) <EOL> value = ( cls , field , operation ) <EOL> pending_lookups . setdefault ( key , [ ] ) . append ( value ) <EOL> def do_pending_lookups ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> key = ( sender . _meta . app_label , sender . __name__ ) <EOL> for cls , field , operation in pending_lookups . pop ( key , [ ] ) : <EOL> operation ( field , sender , cls ) <EOL> signals . class_prepared . connect ( do_pending_lookups ) <EOL> class RelatedField ( object ) : <EOL> def contribute_to_class ( self , cls , name ) : <EOL> sup = super ( RelatedField , self ) <EOL> self . opts = cls . _meta <EOL> if hasattr ( sup , '<STR_LIT>' ) : <EOL> sup . contribute_to_class ( cls , name ) <EOL> if not cls . _meta . abstract and self . rel . related_name : <EOL> self . rel . related_name = self . rel . related_name % { <EOL> '<STR_LIT:class>' : cls . __name__ . lower ( ) , <EOL> '<STR_LIT>' : cls . _meta . app_label . lower ( ) , <EOL> } <EOL> other = self . rel . to <EOL> if isinstance ( other , basestring ) or other . _meta . pk is None : <EOL> def resolve_related_class ( field , model , cls ) : <EOL> field . rel . to = model <EOL> field . do_related_class ( model , cls ) <EOL> add_lazy_relation ( cls , self , other , resolve_related_class ) <EOL> else : <EOL> self . do_related_class ( other , cls ) <EOL> def set_attributes_from_rel ( self ) : <EOL> self . name = self . name or ( self . rel . to . _meta . object_name . lower ( ) + '<STR_LIT:_>' + self . rel . to . _meta . pk . name ) <EOL> if self . verbose_name is None : <EOL> self . verbose_name = self . rel . to . _meta . verbose_name <EOL> self . rel . field_name = self . rel . field_name or self . rel . to . _meta . pk . name <EOL> def do_related_class ( self , other , cls ) : <EOL> self . set_attributes_from_rel ( ) <EOL> self . related = RelatedObject ( other , cls , self ) <EOL> if not cls . _meta . abstract : <EOL> self . contribute_to_related_class ( other , self . related ) <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> return value . prepare ( ) <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> return value . _prepare ( ) <EOL> if lookup_type in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return self . _pk_trace ( value , '<STR_LIT>' , lookup_type ) <EOL> if lookup_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return [ self . _pk_trace ( v , '<STR_LIT>' , lookup_type ) for v in value ] <EOL> elif lookup_type == '<STR_LIT>' : <EOL> return [ ] <EOL> raise TypeError ( "<STR_LIT>" % lookup_type ) <EOL> def get_db_prep_lookup ( self , lookup_type , value , connection , prepared = False ) : <EOL> if not prepared : <EOL> value = self . get_prep_lookup ( lookup_type , value ) <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = value . get_compiler ( connection = connection ) <EOL> if hasattr ( value , '<STR_LIT>' ) or hasattr ( value , '<STR_LIT>' ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> return value <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> sql , params = value . as_sql ( ) <EOL> else : <EOL> sql , params = value . _as_sql ( connection = connection ) <EOL> return QueryWrapper ( ( '<STR_LIT>' % sql ) , params ) <EOL> if lookup_type in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return [ self . _pk_trace ( value , '<STR_LIT>' , lookup_type , <EOL> connection = connection , prepared = prepared ) ] <EOL> if lookup_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return [ self . _pk_trace ( v , '<STR_LIT>' , lookup_type , <EOL> connection = connection , prepared = prepared ) <EOL> for v in value ] <EOL> elif lookup_type == '<STR_LIT>' : <EOL> return [ ] <EOL> raise TypeError ( "<STR_LIT>" % lookup_type ) <EOL> def _pk_trace ( self , value , prep_func , lookup_type , ** kwargs ) : <EOL> v = value <EOL> if isinstance ( v , self . rel . to ) : <EOL> field_name = getattr ( self . rel , "<STR_LIT>" , None ) <EOL> else : <EOL> field_name = None <EOL> try : <EOL> while True : <EOL> if field_name is None : <EOL> field_name = v . _meta . pk . name <EOL> v = getattr ( v , field_name ) <EOL> field_name = None <EOL> except AttributeError : <EOL> pass <EOL> except exceptions . ObjectDoesNotExist : <EOL> v = None <EOL> field = self <EOL> while field . rel : <EOL> if hasattr ( field . rel , '<STR_LIT>' ) : <EOL> field = field . rel . to . _meta . get_field ( field . rel . field_name ) <EOL> else : <EOL> field = field . rel . to . _meta . pk <EOL> if lookup_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> v = [ v ] <EOL> v = getattr ( field , prep_func ) ( lookup_type , v , ** kwargs ) <EOL> if isinstance ( v , list ) : <EOL> v = v [ <NUM_LIT:0> ] <EOL> return v <EOL> def related_query_name ( self ) : <EOL> return self . rel . related_name or self . opts . object_name . lower ( ) <EOL> class SingleRelatedObjectDescriptor ( object ) : <EOL> def __init__ ( self , related ) : <EOL> self . related = related <EOL> self . cache_name = related . get_cache_name ( ) <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> if instance is None : <EOL> return self <EOL> try : <EOL> return getattr ( instance , self . cache_name ) <EOL> except AttributeError : <EOL> params = { '<STR_LIT>' % self . related . field . name : instance . _get_pk_val ( ) } <EOL> db = router . db_for_read ( self . related . model , instance = instance ) <EOL> rel_obj = self . related . model . _base_manager . using ( db ) . get ( ** params ) <EOL> setattr ( instance , self . cache_name , rel_obj ) <EOL> return rel_obj <EOL> def __set__ ( self , instance , value ) : <EOL> if instance is None : <EOL> raise AttributeError ( "<STR_LIT>" % self . related . opts . object_name ) <EOL> if value is None and self . related . field . null == False : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( instance . _meta . object_name , self . related . get_accessor_name ( ) ) ) <EOL> elif value is not None and not isinstance ( value , self . related . model ) : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( value , instance . _meta . object_name , <EOL> self . related . get_accessor_name ( ) , self . related . opts . object_name ) ) <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>' % <EOL> ( value , instance . _state . db , value . _state . db ) ) <EOL> setattr ( value , self . related . field . attname , getattr ( instance , self . related . field . rel . get_related_field ( ) . attname ) ) <EOL> setattr ( instance , self . cache_name , value ) <EOL> setattr ( value , self . related . field . get_cache_name ( ) , instance ) <EOL> class ReverseSingleRelatedObjectDescriptor ( object ) : <EOL> def __init__ ( self , field_with_rel ) : <EOL> self . field = field_with_rel <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> if instance is None : <EOL> return self <EOL> cache_name = self . field . get_cache_name ( ) <EOL> try : <EOL> return getattr ( instance , cache_name ) <EOL> except AttributeError : <EOL> val = getattr ( instance , self . field . attname ) <EOL> if val is None : <EOL> if self . field . null : <EOL> return None <EOL> raise self . field . rel . to . DoesNotExist <EOL> other_field = self . field . rel . get_related_field ( ) <EOL> if other_field . rel : <EOL> params = { '<STR_LIT>' % self . field . rel . field_name : val } <EOL> else : <EOL> params = { '<STR_LIT>' % self . field . rel . field_name : val } <EOL> rel_mgr = self . field . rel . to . _default_manager <EOL> db = router . db_for_read ( self . field . rel . to , instance = instance ) <EOL> if getattr ( rel_mgr , '<STR_LIT>' , False ) : <EOL> rel_obj = rel_mgr . using ( db ) . get ( ** params ) <EOL> else : <EOL> rel_obj = QuerySet ( self . field . rel . to ) . using ( db ) . get ( ** params ) <EOL> setattr ( instance , cache_name , rel_obj ) <EOL> return rel_obj <EOL> def __set__ ( self , instance , value ) : <EOL> if instance is None : <EOL> raise AttributeError ( "<STR_LIT>" % self . _field . name ) <EOL> if value is None and self . field . null == False : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( instance . _meta . object_name , self . field . name ) ) <EOL> elif value is not None and not isinstance ( value , self . field . rel . to ) : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( value , instance . _meta . object_name , <EOL> self . field . name , self . field . rel . to . _meta . object_name ) ) <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>' % <EOL> ( value , instance . _state . db , value . _state . db ) ) <EOL> if value is None : <EOL> related = getattr ( instance , self . field . get_cache_name ( ) , None ) <EOL> if related : <EOL> cache_name = self . field . related . get_cache_name ( ) <EOL> try : <EOL> delattr ( related , cache_name ) <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> val = getattr ( value , self . field . rel . get_related_field ( ) . attname ) <EOL> except AttributeError : <EOL> val = None <EOL> setattr ( instance , self . field . attname , val ) <EOL> setattr ( instance , self . field . get_cache_name ( ) , value ) <EOL> class ForeignRelatedObjectsDescriptor ( object ) : <EOL> def __init__ ( self , related ) : <EOL> self . related = related <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> if instance is None : <EOL> return self <EOL> return self . create_manager ( instance , <EOL> self . related . model . _default_manager . __class__ ) <EOL> def __set__ ( self , instance , value ) : <EOL> if instance is None : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> manager = self . __get__ ( instance ) <EOL> if self . related . field . null : <EOL> manager . clear ( ) <EOL> manager . add ( * value ) <EOL> def delete_manager ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return self . create_manager ( instance , <EOL> self . related . model . _base_manager . __class__ ) <EOL> def create_manager ( self , instance , superclass ) : <EOL> """<STR_LIT>""" <EOL> rel_field = self . related . field <EOL> rel_model = self . related . model <EOL> class RelatedManager ( superclass ) : <EOL> def get_query_set ( self ) : <EOL> db = self . _db or router . db_for_read ( rel_model , instance = instance ) <EOL> return superclass . get_query_set ( self ) . using ( db ) . filter ( ** ( self . core_filters ) ) <EOL> def add ( self , * objs ) : <EOL> for obj in objs : <EOL> if not isinstance ( obj , self . model ) : <EOL> raise TypeError ( "<STR_LIT>" % self . model . _meta . object_name ) <EOL> setattr ( obj , rel_field . name , instance ) <EOL> obj . save ( ) <EOL> add . alters_data = True <EOL> def create ( self , ** kwargs ) : <EOL> kwargs . update ( { rel_field . name : instance } ) <EOL> db = router . db_for_write ( rel_model , instance = 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 . update ( { rel_field . name : instance } ) <EOL> db = router . db_for_write ( rel_model , instance = instance ) <EOL> return super ( RelatedManager , self . db_manager ( db ) ) . get_or_create ( ** kwargs ) <EOL> get_or_create . alters_data = True <EOL> if rel_field . null : <EOL> def remove ( self , * objs ) : <EOL> val = getattr ( instance , rel_field . rel . get_related_field ( ) . attname ) <EOL> for obj in objs : <EOL> if getattr ( obj , rel_field . attname ) == val : <EOL> setattr ( obj , rel_field . name , None ) <EOL> obj . save ( ) <EOL> else : <EOL> raise rel_field . rel . to . DoesNotExist ( "<STR_LIT>" % ( obj , instance ) ) <EOL> remove . alters_data = True <EOL> def clear ( self ) : <EOL> for obj in self . all ( ) : <EOL> setattr ( obj , rel_field . name , None ) <EOL> obj . save ( ) <EOL> clear . alters_data = True <EOL> manager = RelatedManager ( ) <EOL> attname = rel_field . rel . get_related_field ( ) . name <EOL> manager . core_filters = { '<STR_LIT>' % ( rel_field . name , attname ) : <EOL> getattr ( instance , attname ) } <EOL> manager . model = self . related . model <EOL> return manager <EOL> def create_many_related_manager ( superclass , rel = False ) : <EOL> """<STR_LIT>""" <EOL> through = rel . through <EOL> class ManyRelatedManager ( superclass ) : <EOL> def __init__ ( self , model = None , core_filters = None , instance = None , symmetrical = None , <EOL> join_table = None , source_field_name = None , target_field_name = None , <EOL> reverse = False ) : <EOL> super ( ManyRelatedManager , self ) . __init__ ( ) <EOL> self . core_filters = core_filters <EOL> self . model = model <EOL> self . symmetrical = symmetrical <EOL> self . instance = instance <EOL> self . source_field_name = source_field_name <EOL> self . target_field_name = target_field_name <EOL> self . through = through <EOL> self . _pk_val = self . instance . pk <EOL> self . reverse = reverse <EOL> if self . _pk_val is None : <EOL> raise ValueError ( "<STR_LIT>" % instance . __class__ . __name__ ) <EOL> def get_query_set ( self ) : <EOL> db = self . _db or router . db_for_read ( self . instance . __class__ , instance = self . instance ) <EOL> return superclass . get_query_set ( self ) . using ( db ) . _next_is_sticky ( ) . filter ( ** ( self . core_filters ) ) <EOL> if rel . through . _meta . auto_created : <EOL> def add ( self , * objs ) : <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> self . _remove_items ( self . source_field_name , self . target_field_name , * objs ) <EOL> if self . symmetrical : <EOL> self . _remove_items ( self . target_field_name , self . source_field_name , * objs ) <EOL> remove . alters_data = True <EOL> def clear ( self ) : <EOL> self . _clear_items ( self . source_field_name ) <EOL> if self . symmetrical : <EOL> self . _clear_items ( self . target_field_name ) <EOL> clear . alters_data = True <EOL> def create ( self , ** kwargs ) : <EOL> if not rel . through . _meta . auto_created : <EOL> opts = through . _meta <EOL> raise AttributeError ( "<STR_LIT>" % ( opts . app_label , opts . object_name ) ) <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 _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 ( '<STR_LIT>' % <EOL> ( obj , self . instance . _state . db , obj . _state . db ) ) <EOL> new_ids . add ( obj . pk ) <EOL> elif isinstance ( obj , Model ) : <EOL> raise TypeError ( "<STR_LIT>" % self . model . _meta . object_name ) <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 ) . values_list ( target_field_name , flat = True ) <EOL> vals = vals . filter ( ** { <EOL> source_field_name : self . _pk_val , <EOL> '<STR_LIT>' % target_field_name : new_ids , <EOL> } ) <EOL> new_ids = new_ids - set ( vals ) <EOL> if self . reverse or source_field_name == self . source_field_name : <EOL> signals . m2m_changed . send ( sender = rel . through , action = '<STR_LIT>' , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = new_ids , using = db ) <EOL> for obj_id in new_ids : <EOL> self . through . _default_manager . using ( db ) . create ( ** { <EOL> '<STR_LIT>' % source_field_name : self . _pk_val , <EOL> '<STR_LIT>' % target_field_name : obj_id , <EOL> } ) <EOL> if self . reverse or source_field_name == self . source_field_name : <EOL> signals . m2m_changed . send ( sender = rel . through , action = '<STR_LIT>' , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = new_ids , using = db ) <EOL> def _remove_items ( self , source_field_name , target_field_name , * objs ) : <EOL> if objs : <EOL> old_ids = set ( ) <EOL> for obj in objs : <EOL> if isinstance ( obj , self . model ) : <EOL> old_ids . add ( obj . pk ) <EOL> else : <EOL> old_ids . add ( obj ) <EOL> db = router . db_for_write ( self . through , instance = self . instance ) <EOL> if self . reverse or source_field_name == self . source_field_name : <EOL> signals . m2m_changed . send ( sender = rel . through , action = "<STR_LIT>" , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = old_ids , using = db ) <EOL> self . through . _default_manager . using ( db ) . filter ( ** { <EOL> source_field_name : self . _pk_val , <EOL> '<STR_LIT>' % target_field_name : old_ids <EOL> } ) . delete ( ) <EOL> if self . reverse or source_field_name == self . source_field_name : <EOL> signals . m2m_changed . send ( sender = rel . through , action = "<STR_LIT>" , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = old_ids , using = db ) <EOL> def _clear_items ( self , source_field_name ) : <EOL> db = router . db_for_write ( self . through , instance = self . instance ) <EOL> if self . reverse or source_field_name == self . source_field_name : <EOL> signals . m2m_changed . send ( sender = rel . through , action = "<STR_LIT>" , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = None , using = db ) <EOL> self . through . _default_manager . using ( db ) . filter ( ** { <EOL> source_field_name : self . _pk_val <EOL> } ) . delete ( ) <EOL> if self . reverse or source_field_name == self . source_field_name : <EOL> signals . m2m_changed . send ( sender = rel . through , action = "<STR_LIT>" , <EOL> instance = self . instance , reverse = self . reverse , <EOL> model = self . model , pk_set = None , using = db ) <EOL> return ManyRelatedManager <EOL> class ManyRelatedObjectsDescriptor ( object ) : <EOL> def __init__ ( self , related ) : <EOL> self . related = related <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> if instance is None : <EOL> return self <EOL> rel_model = self . related . model <EOL> superclass = rel_model . _default_manager . __class__ <EOL> RelatedManager = create_many_related_manager ( superclass , self . related . field . rel ) <EOL> manager = RelatedManager ( <EOL> model = rel_model , <EOL> core_filters = { '<STR_LIT>' % self . related . field . name : instance . _get_pk_val ( ) } , <EOL> instance = instance , <EOL> symmetrical = False , <EOL> source_field_name = self . related . field . m2m_reverse_field_name ( ) , <EOL> target_field_name = self . related . field . m2m_field_name ( ) , <EOL> reverse = True <EOL> ) <EOL> return manager <EOL> def __set__ ( self , instance , value ) : <EOL> if instance is None : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> if not self . related . field . rel . through . _meta . auto_created : <EOL> opts = self . related . field . rel . through . _meta <EOL> raise AttributeError ( "<STR_LIT>" % ( opts . app_label , opts . object_name ) ) <EOL> manager = self . __get__ ( instance ) <EOL> manager . clear ( ) <EOL> manager . add ( * value ) <EOL> class ReverseManyRelatedObjectsDescriptor ( object ) : <EOL> def __init__ ( self , m2m_field ) : <EOL> self . field = m2m_field <EOL> def _through ( self ) : <EOL> return self . field . rel . through <EOL> through = property ( _through ) <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> if instance is None : <EOL> return self <EOL> rel_model = self . field . rel . to <EOL> superclass = rel_model . _default_manager . __class__ <EOL> RelatedManager = create_many_related_manager ( superclass , self . field . rel ) <EOL> manager = RelatedManager ( <EOL> model = rel_model , <EOL> core_filters = { '<STR_LIT>' % self . field . related_query_name ( ) : instance . _get_pk_val ( ) } , <EOL> instance = instance , <EOL> symmetrical = self . field . rel . symmetrical , <EOL> source_field_name = self . field . m2m_field_name ( ) , <EOL> target_field_name = self . field . m2m_reverse_field_name ( ) , <EOL> reverse = False <EOL> ) <EOL> return manager <EOL> def __set__ ( self , instance , value ) : <EOL> if instance is None : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> if not self . field . rel . through . _meta . auto_created : <EOL> opts = self . field . rel . through . _meta <EOL> raise AttributeError ( "<STR_LIT>" % ( opts . app_label , opts . object_name ) ) <EOL> manager = self . __get__ ( instance ) <EOL> manager . clear ( ) <EOL> manager . add ( * value ) <EOL> class ManyToOneRel ( object ) : <EOL> def __init__ ( self , to , field_name , related_name = None , limit_choices_to = None , <EOL> parent_link = False , on_delete = None ) : <EOL> try : <EOL> to . _meta <EOL> except AttributeError : <EOL> assert isinstance ( to , basestring ) , "<STR_LIT>" % RECURSIVE_RELATIONSHIP_CONSTANT <EOL> self . to , self . field_name = to , field_name <EOL> self . related_name = related_name <EOL> if limit_choices_to is None : <EOL> limit_choices_to = { } <EOL> self . limit_choices_to = limit_choices_to <EOL> self . multiple = True <EOL> self . parent_link = parent_link <EOL> self . on_delete = on_delete <EOL> def is_hidden ( self ) : <EOL> "<STR_LIT>" <EOL> return self . related_name and self . related_name [ - <NUM_LIT:1> ] == '<STR_LIT:+>' <EOL> def get_related_field ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . to . _meta . get_field_by_name ( self . field_name ) <EOL> if not data [ <NUM_LIT:2> ] : <EOL> raise FieldDoesNotExist ( "<STR_LIT>" % <EOL> self . field_name ) <EOL> return data [ <NUM_LIT:0> ] <EOL> class OneToOneRel ( ManyToOneRel ) : <EOL> def __init__ ( self , to , field_name , related_name = None , limit_choices_to = None , <EOL> parent_link = False , on_delete = None ) : <EOL> super ( OneToOneRel , self ) . __init__ ( to , field_name , <EOL> related_name = related_name , limit_choices_to = limit_choices_to , <EOL> parent_link = parent_link , on_delete = on_delete <EOL> ) <EOL> self . multiple = False <EOL> class ManyToManyRel ( object ) : <EOL> def __init__ ( self , to , related_name = None , limit_choices_to = None , <EOL> symmetrical = True , through = None ) : <EOL> self . to = to <EOL> self . related_name = related_name <EOL> if limit_choices_to is None : <EOL> limit_choices_to = { } <EOL> self . limit_choices_to = limit_choices_to <EOL> self . symmetrical = symmetrical <EOL> self . multiple = True <EOL> self . through = through <EOL> def is_hidden ( self ) : <EOL> "<STR_LIT>" <EOL> return self . related_name and self . related_name [ - <NUM_LIT:1> ] == '<STR_LIT:+>' <EOL> def get_related_field ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . to . _meta . pk <EOL> class ForeignKey ( RelatedField , Field ) : <EOL> empty_strings_allowed = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) <EOL> } <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , to , to_field = None , rel_class = ManyToOneRel , ** kwargs ) : <EOL> try : <EOL> to_name = to . _meta . object_name . lower ( ) <EOL> except AttributeError : <EOL> assert isinstance ( to , basestring ) , "<STR_LIT>" % ( self . __class__ . __name__ , to , RECURSIVE_RELATIONSHIP_CONSTANT ) <EOL> else : <EOL> assert not to . _meta . abstract , "<STR_LIT>" % ( self . __class__ . __name__ , to . _meta . object_name ) <EOL> to_field = to_field or ( to . _meta . pk and to . _meta . pk . name ) <EOL> kwargs [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' , None ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> kwargs [ '<STR_LIT>' ] = rel_class ( to , to_field , <EOL> related_name = kwargs . pop ( '<STR_LIT:related_name>' , None ) , <EOL> limit_choices_to = kwargs . pop ( '<STR_LIT>' , None ) , <EOL> parent_link = kwargs . pop ( '<STR_LIT>' , False ) , <EOL> on_delete = kwargs . pop ( '<STR_LIT>' , CASCADE ) , <EOL> ) <EOL> Field . __init__ ( self , ** kwargs ) <EOL> def validate ( self , value , model_instance ) : <EOL> if self . rel . parent_link : <EOL> return <EOL> super ( ForeignKey , self ) . validate ( value , model_instance ) <EOL> if value is None : <EOL> return <EOL> using = router . db_for_read ( model_instance . __class__ , instance = model_instance ) <EOL> qs = self . rel . to . _default_manager . using ( using ) . filter ( <EOL> ** { self . rel . field_name : value } <EOL> ) <EOL> qs = qs . complex_filter ( self . rel . limit_choices_to ) <EOL> if not qs . exists ( ) : <EOL> raise exceptions . ValidationError ( self . error_messages [ '<STR_LIT>' ] % { <EOL> '<STR_LIT>' : self . rel . to . _meta . verbose_name , '<STR_LIT>' : value } ) <EOL> def get_attname ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> def get_validator_unique_lookup_type ( self ) : <EOL> return '<STR_LIT>' % ( self . name , self . rel . get_related_field ( ) . name ) <EOL> def get_default ( self ) : <EOL> "<STR_LIT>" <EOL> field_default = super ( ForeignKey , self ) . get_default ( ) <EOL> if isinstance ( field_default , self . rel . to ) : <EOL> return getattr ( field_default , self . rel . get_related_field ( ) . attname ) <EOL> return field_default <EOL> def get_db_prep_save ( self , value , connection ) : <EOL> if value == '<STR_LIT>' or value == None : <EOL> return None <EOL> else : <EOL> return self . rel . get_related_field ( ) . get_db_prep_save ( value , <EOL> connection = connection ) <EOL> def value_to_string ( self , obj ) : <EOL> if not obj : <EOL> if not self . blank and self . choices : <EOL> choice_list = self . get_choices_default ( ) <EOL> if len ( choice_list ) == <NUM_LIT:2> : <EOL> return smart_unicode ( choice_list [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> return Field . value_to_string ( self , obj ) <EOL> def contribute_to_class ( self , cls , name ) : <EOL> super ( ForeignKey , self ) . contribute_to_class ( cls , name ) <EOL> setattr ( cls , self . name , ReverseSingleRelatedObjectDescriptor ( self ) ) <EOL> if isinstance ( self . rel . to , basestring ) : <EOL> target = self . rel . to <EOL> else : <EOL> target = self . rel . to . _meta . db_table <EOL> cls . _meta . duplicate_targets [ self . column ] = ( target , "<STR_LIT>" ) <EOL> def contribute_to_related_class ( self , cls , related ) : <EOL> if not self . rel . is_hidden ( ) : <EOL> setattr ( cls , related . get_accessor_name ( ) , ForeignRelatedObjectsDescriptor ( related ) ) <EOL> if self . rel . limit_choices_to : <EOL> cls . _meta . related_fkey_lookups . append ( self . rel . limit_choices_to ) <EOL> if self . rel . field_name is None : <EOL> self . rel . field_name = cls . _meta . pk . name <EOL> def formfield ( self , ** kwargs ) : <EOL> db = kwargs . pop ( '<STR_LIT>' , None ) <EOL> defaults = { <EOL> '<STR_LIT>' : forms . ModelChoiceField , <EOL> '<STR_LIT>' : self . rel . to . _default_manager . using ( db ) . complex_filter ( self . rel . limit_choices_to ) , <EOL> '<STR_LIT>' : self . rel . field_name , <EOL> } <EOL> defaults . update ( kwargs ) <EOL> return super ( ForeignKey , self ) . formfield ( ** defaults ) <EOL> def db_type ( self , connection ) : <EOL> rel_field = self . rel . get_related_field ( ) <EOL> if ( isinstance ( rel_field , AutoField ) or <EOL> ( not connection . features . related_fields_match_type and <EOL> isinstance ( rel_field , ( PositiveIntegerField , <EOL> PositiveSmallIntegerField ) ) ) ) : <EOL> return IntegerField ( ) . db_type ( connection = connection ) <EOL> return rel_field . db_type ( connection = connection ) <EOL> class OneToOneField ( ForeignKey ) : <EOL> """<STR_LIT>""" <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , to , to_field = None , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> super ( OneToOneField , self ) . __init__ ( to , to_field , OneToOneRel , ** kwargs ) <EOL> def contribute_to_related_class ( self , cls , related ) : <EOL> setattr ( cls , related . get_accessor_name ( ) , <EOL> SingleRelatedObjectDescriptor ( related ) ) <EOL> def formfield ( self , ** kwargs ) : <EOL> if self . rel . parent_link : <EOL> return None <EOL> return super ( OneToOneField , self ) . formfield ( ** kwargs ) <EOL> def save_form_data ( self , instance , data ) : <EOL> if isinstance ( data , self . rel . to ) : <EOL> setattr ( instance , self . name , data ) <EOL> else : <EOL> setattr ( instance , self . attname , data ) <EOL> def create_many_to_many_intermediary_model ( field , klass ) : <EOL> from django . db import models <EOL> managed = True <EOL> if isinstance ( field . rel . to , basestring ) and field . rel . to != RECURSIVE_RELATIONSHIP_CONSTANT : <EOL> to_model = field . rel . to <EOL> to = to_model . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> def set_managed ( field , model , cls ) : <EOL> field . rel . through . _meta . managed = model . _meta . managed or cls . _meta . managed <EOL> add_lazy_relation ( klass , field , to_model , set_managed ) <EOL> elif isinstance ( field . rel . to , basestring ) : <EOL> to = klass . _meta . object_name <EOL> to_model = klass <EOL> managed = klass . _meta . managed <EOL> else : <EOL> to = field . rel . to . _meta . object_name <EOL> to_model = field . rel . to <EOL> managed = klass . _meta . managed or to_model . _meta . managed <EOL> name = '<STR_LIT>' % ( klass . _meta . object_name , field . name ) <EOL> if field . rel . to == RECURSIVE_RELATIONSHIP_CONSTANT or to == klass . _meta . object_name : <EOL> from_ = '<STR_LIT>' % to . lower ( ) <EOL> to = '<STR_LIT>' % to . lower ( ) <EOL> else : <EOL> from_ = klass . _meta . object_name . lower ( ) <EOL> to = to . lower ( ) <EOL> meta = type ( '<STR_LIT:Meta>' , ( object , ) , { <EOL> '<STR_LIT>' : field . _get_m2m_db_table ( klass . _meta ) , <EOL> '<STR_LIT>' : managed , <EOL> '<STR_LIT>' : klass , <EOL> '<STR_LIT>' : klass . _meta . app_label , <EOL> '<STR_LIT>' : ( from_ , to ) , <EOL> '<STR_LIT>' : '<STR_LIT>' % { '<STR_LIT>' : from_ , '<STR_LIT:to>' : to } , <EOL> '<STR_LIT>' : '<STR_LIT>' % { '<STR_LIT>' : from_ , '<STR_LIT:to>' : to } , <EOL> } ) <EOL> return type ( name , ( models . Model , ) , { <EOL> '<STR_LIT:Meta>' : meta , <EOL> '<STR_LIT>' : klass . __module__ , <EOL> from_ : models . ForeignKey ( klass , related_name = '<STR_LIT>' % name ) , <EOL> to : models . ForeignKey ( to_model , related_name = '<STR_LIT>' % name ) <EOL> } ) <EOL> class ManyToManyField ( RelatedField , Field ) : <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , to , ** kwargs ) : <EOL> try : <EOL> assert not to . _meta . abstract , "<STR_LIT>" % ( self . __class__ . __name__ , to . _meta . object_name ) <EOL> except AttributeError : <EOL> assert isinstance ( to , basestring ) , "<STR_LIT>" % ( self . __class__ . __name__ , to , RECURSIVE_RELATIONSHIP_CONSTANT ) <EOL> kwargs [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' , None ) <EOL> kwargs [ '<STR_LIT>' ] = ManyToManyRel ( to , <EOL> related_name = kwargs . pop ( '<STR_LIT:related_name>' , None ) , <EOL> limit_choices_to = kwargs . pop ( '<STR_LIT>' , None ) , <EOL> symmetrical = kwargs . pop ( '<STR_LIT>' , to == RECURSIVE_RELATIONSHIP_CONSTANT ) , <EOL> through = kwargs . pop ( '<STR_LIT>' , None ) ) <EOL> self . db_table = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if kwargs [ '<STR_LIT>' ] . through is not None : <EOL> assert self . db_table is None , "<STR_LIT>" <EOL> Field . __init__ ( self , ** kwargs ) <EOL> msg = _ ( '<STR_LIT>' ) <EOL> self . help_text = string_concat ( self . help_text , '<STR_LIT:U+0020>' , msg ) <EOL> def get_choices_default ( self ) : <EOL> return Field . get_choices ( self , include_blank = False ) <EOL> def _get_m2m_db_table ( self , opts ) : <EOL> "<STR_LIT>" <EOL> if self . rel . through is not None : <EOL> return self . rel . through . _meta . db_table <EOL> elif self . db_table : <EOL> return self . db_table <EOL> else : <EOL> return util . truncate_name ( '<STR_LIT>' % ( opts . db_table , self . name ) , <EOL> connection . ops . max_name_length ( ) ) <EOL> def _get_m2m_attr ( self , related , attr ) : <EOL> "<STR_LIT>" <EOL> cache_attr = '<STR_LIT>' % attr <EOL> if hasattr ( self , cache_attr ) : <EOL> return getattr ( self , cache_attr ) <EOL> for f in self . rel . through . _meta . fields : <EOL> if hasattr ( f , '<STR_LIT>' ) and f . rel and f . rel . to == related . model : <EOL> setattr ( self , cache_attr , getattr ( f , attr ) ) <EOL> return getattr ( self , cache_attr ) <EOL> def _get_m2m_reverse_attr ( self , related , attr ) : <EOL> "<STR_LIT>" <EOL> cache_attr = '<STR_LIT>' % attr <EOL> if hasattr ( self , cache_attr ) : <EOL> return getattr ( self , cache_attr ) <EOL> found = False <EOL> for f in self . rel . through . _meta . fields : <EOL> if hasattr ( f , '<STR_LIT>' ) and f . rel and f . rel . to == related . parent_model : <EOL> if related . model == related . parent_model : <EOL> if found : <EOL> setattr ( self , cache_attr , getattr ( f , attr ) ) <EOL> break <EOL> else : <EOL> found = True <EOL> else : <EOL> setattr ( self , cache_attr , getattr ( f , attr ) ) <EOL> break <EOL> return getattr ( self , cache_attr ) <EOL> def value_to_string ( self , obj ) : <EOL> data = '<STR_LIT>' <EOL> if obj : <EOL> qs = getattr ( obj , self . name ) . all ( ) <EOL> data = [ instance . _get_pk_val ( ) for instance in qs ] <EOL> else : <EOL> if not self . blank : <EOL> choices_list = self . get_choices_default ( ) <EOL> if len ( choices_list ) == <NUM_LIT:1> : <EOL> data = [ choices_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ] <EOL> return smart_unicode ( data ) <EOL> def contribute_to_class ( self , cls , name ) : <EOL> if self . rel . symmetrical and ( self . rel . to == "<STR_LIT>" or self . rel . to == cls . _meta . object_name ) : <EOL> self . rel . related_name = "<STR_LIT>" % name <EOL> super ( ManyToManyField , self ) . contribute_to_class ( cls , name ) <EOL> if not self . rel . through and not cls . _meta . abstract : <EOL> self . rel . through = create_many_to_many_intermediary_model ( self , cls ) <EOL> setattr ( cls , self . name , ReverseManyRelatedObjectsDescriptor ( self ) ) <EOL> self . m2m_db_table = curry ( self . _get_m2m_db_table , cls . _meta ) <EOL> if isinstance ( self . rel . through , basestring ) : <EOL> def resolve_through_model ( field , model , cls ) : <EOL> field . rel . through = model <EOL> add_lazy_relation ( cls , self , self . rel . through , resolve_through_model ) <EOL> if isinstance ( self . rel . to , basestring ) : <EOL> target = self . rel . to <EOL> else : <EOL> target = self . rel . to . _meta . db_table <EOL> cls . _meta . duplicate_targets [ self . column ] = ( target , "<STR_LIT>" ) <EOL> def contribute_to_related_class ( self , cls , related ) : <EOL> if not self . rel . is_hidden ( ) : <EOL> setattr ( cls , related . get_accessor_name ( ) , ManyRelatedObjectsDescriptor ( related ) ) <EOL> self . m2m_column_name = curry ( self . _get_m2m_attr , related , '<STR_LIT>' ) <EOL> self . m2m_reverse_name = curry ( self . _get_m2m_reverse_attr , related , '<STR_LIT>' ) <EOL> self . m2m_field_name = curry ( self . _get_m2m_attr , related , '<STR_LIT:name>' ) <EOL> self . m2m_reverse_field_name = curry ( self . _get_m2m_reverse_attr , related , '<STR_LIT:name>' ) <EOL> get_m2m_rel = curry ( self . _get_m2m_attr , related , '<STR_LIT>' ) <EOL> self . m2m_target_field_name = lambda : get_m2m_rel ( ) . field_name <EOL> get_m2m_reverse_rel = curry ( self . _get_m2m_reverse_attr , related , '<STR_LIT>' ) <EOL> self . m2m_reverse_target_field_name = lambda : get_m2m_reverse_rel ( ) . field_name <EOL> def set_attributes_from_rel ( self ) : <EOL> pass <EOL> def value_from_object ( self , obj ) : <EOL> "<STR_LIT>" <EOL> return getattr ( obj , self . attname ) . all ( ) <EOL> def save_form_data ( self , instance , data ) : <EOL> setattr ( instance , self . attname , data ) <EOL> def formfield ( self , ** kwargs ) : <EOL> db = kwargs . pop ( '<STR_LIT>' , None ) <EOL> defaults = { <EOL> '<STR_LIT>' : forms . ModelMultipleChoiceField , <EOL> '<STR_LIT>' : self . rel . to . _default_manager . using ( db ) . complex_filter ( self . rel . limit_choices_to ) <EOL> } <EOL> defaults . update ( kwargs ) <EOL> if defaults . get ( '<STR_LIT>' ) is not None : <EOL> initial = defaults [ '<STR_LIT>' ] <EOL> if callable ( initial ) : <EOL> initial = initial ( ) <EOL> defaults [ '<STR_LIT>' ] = [ i . _get_pk_val ( ) for i in initial ] <EOL> return super ( ManyToManyField , self ) . formfield ( ** defaults ) <EOL> def db_type ( self , connection ) : <EOL> return None </s>
