
TRNc           @   sg  d  d l  m Z d  d l m Z m Z m Z d  d l m Z d  d l m	 Z	 m
 Z
 d  d l m Z m Z m Z m Z m Z m Z d  d l m Z d  d l m Z d  d l m Z d  d	 l m Z d  d
 l m Z m Z m Z m Z d  d l  m! Z! d  d l" m# Z# d  d l$ m% Z% d Z& i  Z' d   Z( d   Z) e	 j* j+ e)  d e, f d     YZ- d e, f d     YZ. d e, f d     YZ/ d e, f d     YZ0 e1 d  Z2 d e, f d     YZ3 d e, f d     YZ4 d e, f d     YZ5 d  e5 f d!     YZ6 d" e, f d#     YZ7 d$ e- e f d%     YZ8 d& e8 f d'     YZ9 d(   Z: d) e- e f d*     YZ; d+ S(,   i(   t   settings(   t
   connectiont   routert   transaction(   t   util(   t   signalst	   get_model(   t	   AutoFieldt   Fieldt   IntegerFieldt   PositiveIntegerFieldt   PositiveSmallIntegerFieldt   FieldDoesNotExist(   t   RelatedObject(   t   QuerySet(   t   QueryWrapper(   t   smart_unicode(   t   ugettext_lazyt   string_concatt	   ungettextt   ugettext(   t   curry(   t
   exceptions(   t   formst   selfc   	      C   s   | t  k r$ |  j j } |  j } ng y | j d  \ } } WnK t k
 rb |  j j } | } n) t k
 r | j j } | j j } n Xt | | t	  } | r | | | |   n4 | | f } |  | | f } t
 j | g   j |  d S(   s<  
    Adds a lookup on ``cls`` when a related field is defined using a string,
    i.e.::

        class MyModel(Model):
            fk = ForeignKey("AnotherModel")

    This string can be:

        * RECURSIVE_RELATIONSHIP_CONSTANT (i.e. "self") to indicate a recursive
          relation.

        * The name of a model (i.e "AnotherModel") to indicate another model in
          the same app.

        * An app-label and model name (i.e. "someapp.AnotherModel") to indicate
          another model in a different app.

    If the other model hasn't yet been loaded -- almost a given if you're using
    lazy relationships -- then the relation won't be set up until the
    class_prepared signal fires at the end of model initialization.

    operation is the work that must be performed once the relation can be resolved.
    t   .N(   t   RECURSIVE_RELATIONSHIP_CONSTANTt   _metat	   app_labelt   __name__t   splitt
   ValueErrort   AttributeErrort   object_nameR   t   Falset   pending_lookupst
   setdefaultt   append(	   t   clst   fieldt   relationt	   operationR   t
   model_namet   modelt   keyt   value(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   add_lazy_relation   s"    	c         K   sO   |  j  j |  j f } x3 t j | g   D] \ } } } | | |  |  q( Wd S(   sV   
    Handle any pending relations to the sending model. Sent from class_prepared.
    N(   R   R   R   R#   t   pop(   t   sendert   kwargsR,   R&   R'   R)   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   do_pending_lookupsL   s    "t   RelatedFieldc           B   sJ   e  Z d    Z d   Z d   Z d   Z e d  Z d   Z d   Z	 RS(   c         C   s   t  t |   } | j |  _ t | d  r= | j | |  n  | j j r |  j j r |  j j i | j	 j
   d 6| j j j
   d 6|  j _ n  |  j j } t | t  s | j j d  k r d   } t | |  | |  n |  j | |  d  S(   Nt   contribute_to_classt   classR   c         S   s    | |  j  _ |  j | |  d  S(   N(   t   relt   tot   do_related_class(   R'   R+   R&   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   resolve_related_classi   s    (   t   superR3   R   t   optst   hasattrR4   t   abstractR6   t   related_nameR   t   lowerR   R7   t
   isinstancet
   basestringt   pkt   NoneR.   R8   (   R   R&   t   namet   supt   otherR9   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR4   X   s     !	c         C   s   |  j  p5 |  j j j j j   d |  j j j j j  |  _  |  j d  k rb |  j j j j |  _ n  |  j j	 p |  j j j j j  |  j _	 d  S(   Nt   _(
   RD   R6   R7   R   R!   R?   RB   t   verbose_nameRC   t
   field_name(   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   set_attributes_from_relp   s    ;c         C   sE   |  j    t | | |   |  _ | j j sA |  j | |  j  n  d  S(   N(   RJ   R   t   relatedR   R=   t   contribute_to_related_class(   R   RF   R&   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR8   v   s    
c         C   s   t  | d  r | j   St  | d  r2 | j   S| d k rQ |  j | d |  S| d k r g  | D] } |  j | d |  ^ qd S| d k r g  St d |   d  S(   Nt   preparet   _preparet   exactt   gtt   ltt   gtet   ltet   get_prep_lookupt   ranget   int   isnulls$   Related Field has invalid lookup: %s(   s   exacts   gts   lts   gtes   lte(   s   ranges   in(   R<   RM   RN   t	   _pk_tracet	   TypeError(   R   t   lookup_typeR-   t   v(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRT   |   s    

&c         C   sM  | s |  j  | |  } n  t | d  r? | j d |  } n  t | d  s] t | d  r t | d  rp | St | d  r | j   \ } } n | j d |  \ } } t d | |  S| d k r |  j | d | d | d | g S| d k r)g  | D]' } |  j | d | d | d | ^ q S| d k r9g  St d |   d  S(   Nt   get_compilerR   t   as_sqlt   _as_sqlt   relabel_aliasess   (%s)RO   RP   RQ   RR   RS   t   get_db_prep_lookupt   preparedRU   RV   RW   s$   Related Field has invalid lookup: %s(   s   exacts   gts   lts   gtes   lte(   s   ranges   in(   RT   R<   R\   R]   R^   R   RX   RY   (   R   RZ   R-   R   Ra   t   sqlt   paramsR[   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR`      s(    /c         K   s
  | } y) x" t  r- t | | j j j  } q WWn* t k
 rB n t j k
 r[ d  } n X|  } xR | j	 r t
 | j	 d  r | j	 j j j | j	 j  } qe | j	 j j j } qe W| d k r | g } n  t | |  | | |  } t | t  r| d } n  | S(   NRI   RU   RV   i    (   s   ranges   in(   t   Truet   getattrR   RB   RD   R    R   t   ObjectDoesNotExistRC   R6   R<   R7   t	   get_fieldRI   R@   t   list(   R   R-   t	   prep_funcRZ   R1   R[   R'   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRX      s&    	 
!c         C   s   |  j  j p |  j j j   S(   N(   R6   R>   R;   R!   R?   (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   related_query_name   s    (
   R   t
   __module__R4   RJ   R8   RT   R"   R`   RX   Rj   (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR3   W   s   					t   SingleRelatedObjectDescriptorc           B   s&   e  Z d    Z d d  Z d   Z RS(   c         C   s   | |  _  | j   |  _ d  S(   N(   RK   t   get_cache_namet
   cache_name(   R   RK   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   __init__   s    	c         C   s   | d  k r |  Sy t | |  j  SWn t k
 r i | j   d |  j j j 6} t j	 |  j j
 d | } |  j j
 j j |  j |   } t | |  j |  | SXd  S(   Ns   %s__pkt   instance(   RC   Re   Rn   R    t   _get_pk_valRK   R'   RD   R   t   db_for_readR+   t   _base_managert   usingt   gett   setattr(   R   Rp   t   instance_typeRc   t   dbt   rel_obj(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   __get__   s     !c      	   C   s  | d  k r( t d |  j j j   n  | d  k rq |  j j j t k rq t d | j	 j |  j j
   f   n/| d  k	 r t | |  j j  r t d | | j	 j |  j j
   |  j j j f   n | d  k	 r| j j d  k r	t j | j d | | j _ q| j j d  k r<t j | j d | | j _ q| j j d  k	 r| j j d  k	 rt j | |  st d | | j j | j j f   qqn  t | |  j j j t | |  j j j j   j   t | |  j |  t | |  j j j   |  d  S(   Ns    %s must be accessed via instances7   Cannot assign None: "%s.%s" does not allow null values.s4   Cannot assign "%r": "%s.%s" must be a "%s" instance.Rp   sK   Cannot assign "%r": instance is on database "%s", value is on database "%s"(   RC   R    RK   R;   R!   R'   t   nullR"   R   R   t   get_accessor_nameR@   R+   t   _stateRx   R   t   db_for_writet	   __class__t   allow_relationRv   t   attnameRe   R6   t   get_related_fieldRn   Rm   (   R   Rp   R-   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   __set__   s*    !""%!!$(4N(   R   Rk   Ro   RC   Rz   R   (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRl      s   	t$   ReverseSingleRelatedObjectDescriptorc           B   s&   e  Z d    Z d d  Z d   Z RS(   c         C   s   | |  _  d  S(   N(   R'   (   R   t   field_with_rel(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo     s    c   
      C   su  | d  k r |  S|  j j   } y t | |  SWn>t k
 rpt | |  j j  } | d  k r |  j j rq d  S|  j j j j	  n  |  j j j
   } | j r i | d |  j j j 6} n i | d |  j j j 6} |  j j j j } t j |  j j j d | } t | d t  r5| j |  j |   }	 n' t |  j j j  j |  j |   }	 t | | |	  |	 SXd  S(   Ns   %s__pks	   %s__exactRp   t   use_for_related_fields(   RC   R'   Rm   Re   R    R   R{   R6   R7   t   DoesNotExistR   RI   t   _default_managerR   Rr   R"   Rt   Ru   R   Rv   (
   R   Rp   Rw   Rn   t   valt   other_fieldRc   t   rel_mgrRx   Ry   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRz     s,    	'c         C   sv  | d  k r% t d |  j j   n  | d  k rh |  j j t k rh t d | j j	 |  j j f   n5| d  k	 r t
 | |  j j j  r t d | | j j	 |  j j |  j j j j j	 f   n | d  k	 r| j j d  k rt j | j d | | j _ q| j j d  k r9t j | j d | | j _ q| j j d  k	 r| j j d  k	 rt j | |  st d | | j j | j j f   qqn  | d  k rt | |  j j   d   } | r|  j j j   } y t | |  Wqt k
 r qXqn  y" t | |  j j j   j  } Wn t k
 rBd  } n Xt | |  j j |  t | |  j j   |  d  S(   Ns    %s must be accessed via instances7   Cannot assign None: "%s.%s" does not allow null values.s4   Cannot assign "%r": "%s.%s" must be a "%s" instance.Rp   sK   Cannot assign "%r": instance is on database "%s", value is on database "%s"(   RC   R    t   _fieldRD   R'   R{   R"   R   R   R!   R@   R6   R7   R}   Rx   R   R~   R   R   Re   Rm   RK   t   delattrR   R   Rv   (   R   Rp   R-   RK   Rn   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   2  s@    %(!!$(
"
N(   R   Rk   Ro   RC   Rz   R   (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   
  s   	t   ForeignRelatedObjectsDescriptorc           B   s8   e  Z d    Z d d  Z d   Z d   Z d   Z RS(   c         C   s   | |  _  d  S(   N(   RK   (   R   RK   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo   r  s    c         C   s,   | d  k r |  S|  j | |  j j j j  S(   N(   RC   t   create_managerRK   R+   R   R   (   R   Rp   Rw   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRz   u  s    	c         C   sW   | d  k r t d   n  |  j |  } |  j j j rF | j   n  | j |   d  S(   Ns%   Manager must be accessed via instance(   RC   R    Rz   RK   R'   R{   t   cleart   add(   R   Rp   R-   t   manager(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   |  s    c         C   s   |  j  | |  j j j j  S(   s   
        Returns a queryset based on the related model's base manager (rather
        than the default manager, as returned by __get__). Used by
        Model.delete().
        (   R   RK   R+   Rs   R   (   R   Rp   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   delete_manager  s    	c            s   |  j  j  |  j  j  d  f       f d     Y      }  j j   j } i t  |  d  j | f 6| _ |  j  j | _ | S(   sV   
        Creates the managers used by other methods (__get__() and delete()).
        t   RelatedManagerc              s   e  Z    f d    Z   f d   Z e e _      f d   Z e e _      f d   Z e e _  j r   f d   Z	 e e	 _  f d   Z
 e e
 _ n  RS(   c            s@   |  j  p t j  d   }  j |   j |  j |  j   S(   NRp   (   t   _dbR   Rr   t   get_query_setRt   t   filtert   core_filters(   R   Rx   (   Rp   t
   superclasst	   rel_model(    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    c            s`   xY | D]Q } t  | |  j  s; t d |  j j j   n  t |  j    | j   q Wd  S(   Ns   '%s' instance expected(   R@   R+   RY   R   R!   Rv   RD   t   save(   R   t   objst   obj(   Rp   t	   rel_field(    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s
    c            sK   | j  i   j 6 t j  d  } t   |   j |  j |   S(   NRp   (   t   updateRD   R   R~   R:   Rt   t   create(   R   R1   Rx   (   R   Rp   R   R   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    c            sK   | j  i   j 6 t j  d  } t   |   j |  j |   S(   NRp   (   R   RD   R   R~   R:   Rt   t   get_or_create(   R   R1   Rx   (   R   Rp   R   R   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    c            s   t     j j   j  } xe | D]] } t  |  j  | k r` t |  j d   | j   q"  j j j	 d |   f   q" Wd  S(   Ns   %r is not related to %r.(
   Re   R6   R   R   Rv   RD   RC   R   R7   R   (   R   R   R   R   (   Rp   R   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   remove  s    c            s8   x1 |  j    D]# } t |   j d   | j   q Wd  S(   N(   t   allRv   RD   RC   R   (   R   R   (   R   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    (   R   Rk   R   R   Rd   t   alters_dataR   R   R{   R   R   (    (   R   Rp   R   R   R   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s   						s   %s__%s(   RK   R'   R+   R6   R   RD   Re   R   (   R   Rp   R   R   R   (    (   R   Rp   R   R   R   sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    (.	#N(   R   Rk   Ro   RC   Rz   R   R   R   (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   l  s
   				c            s2    j   d  f      f d     Y    S(   s{   Creates a manager that subclasses 'superclass' (which is a Manager)
    and adds behavior for many-to-many related objects.t   ManyRelatedManagerc        
      s   e  Z d
 d
 d
 d
 d
 d
 d
 e    f d   Z  f d   Z  j j j ru d   Z	 e
 e	 _ d   Z e
 e _ n  d   Z e
 e _     f d   Z e
 e _   f d   Z e
 e _  f d   Z  f d   Z  f d	   Z RS(   c	   	         s   t    |   j   | |  _ | |  _ | |  _ | |  _ | |  _ | |  _  |  _ |  j j	 |  _
 | |  _ |  j
 d  k r t d | j j   n  d  S(   Ns]   %r instance needs to have a primary key value before a many-to-many relationship can be used.(   R:   Ro   R   R+   t   symmetricalRp   t   source_field_namet   target_field_namet   throughRB   t   _pk_valt   reverseRC   R   R   R   (	   R   R+   R   Rp   R   t
   join_tableR   R   R   (   R   R   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo     s    								c            sO   |  j  p$ t j |  j j d |  j }   j |   j |  j   j |  j	   S(   NRp   (
   R   R   Rr   Rp   R   R   Rt   t   _next_is_stickyR   R   (   R   Rx   (   R   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    'c         W   sB   |  j  |  j |  j |  |  j r> |  j  |  j |  j |  n  d  S(   N(   t
   _add_itemsR   R   R   (   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    	c         W   sB   |  j  |  j |  j |  |  j r> |  j  |  j |  j |  n  d  S(   N(   t   _remove_itemsR   R   R   (   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    	c         S   s0   |  j  |  j  |  j r, |  j  |  j  n  d  S(   N(   t   _clear_itemsR   R   R   (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    	c            s    j  j j s7  j } t d | j | j f   n  t j |  j j	 d |  j } t
   |   j |  j |   } |  j |  | S(   Nsl   Cannot use create() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.Rp   (   R   R   t   auto_createdR    R   R!   R   R~   Rp   R   R:   Rt   R   R   (   R   R1   R;   Rx   t   new_obj(   R   R   R6   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    	!c            se   t  j |  j j d |  j } t   |   j |  j |   \ } } | r[ |  j |  n  | | f S(   NRp   (   R   R~   Rp   R   R:   Rt   R   R   (   R   R1   Rx   R   t   created(   R   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s
    'c   
         s@  d d l  m } | r<t   } x | D] } t | |  j  r t j | |  j  s~ t d | |  j j	 j
 | j	 j
 f   n  | j | j  q& t | |  r t d |  j j j   q& | j |  q& Wt j |  j j d |  j } |  j j j |  j | d t } | j i |  j | 6| d | 6  } | t |  } |  j s[| |  j k rt j j d   j d	 d
 d |  j d |  j d |  j d |  n  xC | D]; }	 |  j j j |  j i |  j d | 6|	 d | 6  qW|  j s| |  j k r<t j j d   j d	 d d |  j d |  j d |  j d |  q<n  d  S(   Ni(   t   ModelsH   Cannot add "%r": instance is on database "%s", value is on database "%s"s   '%s' instance expectedRp   t   flats   %s__inR0   t   actiont   pre_addR   R+   t   pk_sets   %s_idt   post_add(   t   django.db.modelsR   t   setR@   R+   R   R   Rp   R   R}   Rx   R   RB   RY   R   R!   R~   R   R   R   Rt   t   values_listRd   R   R   R   R   R   t   m2m_changedt   sendR   (
   R   R   R   R   R   t   new_idsR   Rx   t   valst   obj_id(   R6   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s<    	%$	
c            s^  | rZt    } x@ | D]8 } t | |  j  rA | j | j  q | j |  q W|  j sj | |  j k r t j j	 d   j
 d d d |  j d |  j d |  j d |  n  t j |  j
 j d |  j } |  j
 j j |  j i |  j | 6| d | 6  j   |  j s| |  j k rZt j j	 d   j
 d d	 d |  j d |  j d |  j d |  qZn  d  S(
   NR0   R   t
   pre_removeRp   R   R+   R   s   %s__int   post_remove(   R   R@   R+   R   RB   R   R   R   R   R   R   Rp   R   R~   R   R   Rt   R   R   t   delete(   R   R   R   R   t   old_idsR   Rx   (   R6   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   G  s$    	
c            s   |  j  s | |  j k rX t j j d   j d d d |  j d |  j  d |  j d d   n  t	 j
 |  j j d |  j } |  j j j |  j i |  j | 6  j   |  j  s | |  j k r t j j d   j d d d |  j d |  j  d |  j d d   n  d  S(	   NR0   R   t	   pre_clearRp   R   R+   R   t
   post_clear(   R   R   R   R   R   R   Rp   R+   RC   R   R~   R   R   Rt   R   R   R   (   R   R   Rx   (   R6   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   h  s    N(   R   Rk   RC   R"   Ro   R   R   R   R   R   Rd   R   R   R   R   R   R   R   R   (    (   R   R   R   R6   (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s$   						
			/!(   R   (   R   R6   (    (   R   R   R   R6   sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   create_many_related_manager  s    	%t   ManyRelatedObjectsDescriptorc           B   s&   e  Z d    Z d d  Z d   Z RS(   c         C   s   | |  _  d  S(   N(   RK   (   R   RK   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo     s    c         C   s   | d  k r |  S|  j j } | j j } t | |  j j j  } | d | d i | j   d |  j j j	 6d | d t
 d |  j j j   d |  j j j   d t  } | S(	   NR+   R   s   %s__pkRp   R   R   R   R   (   RC   RK   R+   R   R   R   R'   R6   Rq   RD   R"   t   m2m_reverse_field_namet   m2m_field_nameRd   (   R   Rp   Rw   R   R   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRz     s     	c         C   s   | d  k r t d   n  |  j j j j j j sg |  j j j j j } t d | j | j	 f   n  |  j
 |  } | j   | j |   d  S(   Ns%   Manager must be accessed via instancesj   Cannot set values on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead.(   RC   R    RK   R'   R6   R   R   R   R   R!   Rz   R   R   (   R   Rp   R-   R;   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    
N(   R   Rk   Ro   RC   Rz   R   (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   }  s   	t#   ReverseManyRelatedObjectsDescriptorc           B   s;   e  Z d    Z d   Z e e  Z d d  Z d   Z RS(   c         C   s   | |  _  d  S(   N(   R'   (   R   t	   m2m_field(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo     s    c         C   s   |  j  j j S(   N(   R'   R6   R   (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   _through  s    c         C   s   | d  k r |  S|  j j j } | j j } t | |  j j  } | d | d i | j   d |  j j   6d | d |  j j j	 d |  j j
   d |  j j   d t  } | S(	   NR+   R   s   %s__pkRp   R   R   R   R   (   RC   R'   R6   R7   R   R   R   Rq   Rj   R   R   R   R"   (   R   Rp   Rw   R   R   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRz     s     	c         C   s   | d  k r t d   n  |  j j j j j sa |  j j j j } t d | j | j f   n  |  j	 |  } | j
   | j |   d  S(   Ns%   Manager must be accessed via instancesk   Cannot set values on a ManyToManyField which specifies an intermediary model.  Use %s.%s's Manager instead.(   RC   R    R'   R6   R   R   R   R   R!   Rz   R   R   (   R   Rp   R-   R;   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    
N(	   R   Rk   Ro   R   t   propertyR   RC   Rz   R   (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s
   		t   ManyToOneRelc           B   s/   e  Z d d d e d   Z d   Z d   Z RS(   c         C   s   y | j  Wn- t k
 r: t | t  s; t d t  n X| | |  _ |  _ | |  _ | d  k rl i  } n  | |  _
 | p~ i  |  _ t |  _ | |  _ d  S(   Ns:   'to' must be either a model, a model name or the string %r(   R   R    R@   RA   t   AssertionErrorR   R7   RI   R>   RC   t   limit_choices_tot   lookup_overridesRd   t   multiplet   parent_link(   R   R7   RI   R>   R   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo     s     				c         C   s   |  j  o |  j  d d k S(   s$   Should the related object be hidden?it   +(   R>   (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt	   is_hidden  s    c         C   s@   |  j  j j |  j  } | d s8 t d |  j   n  | d S(   sb   
        Returns the Field in the 'to' object to which this relationship is
        tied.
        i   s   No related field named '%s'i    (   R7   R   t   get_field_by_nameRI   R   (   R   t   data(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s
    
N(   R   Rk   RC   R"   Ro   R   R   (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s   	t   OneToOneRelc           B   s   e  Z d d d e d   Z RS(   c         C   s>   t  t |   j | | d | d | d | d | t |  _ d  S(   NR>   R   R   R   (   R:   R   Ro   R"   R   (   R   R7   RI   R>   R   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo     s    N(   R   Rk   RC   R"   Ro   (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s   t   ManyToManyRelc           B   s/   e  Z d d e d d   Z d   Z d   Z RS(   c         C   sO   | |  _  | |  _ | d  k r' i  } n  | |  _ | |  _ t |  _ | |  _ d  S(   N(   R7   R>   RC   R   R   Rd   R   R   (   R   R7   R>   R   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo     s    						c         C   s   |  j  o |  j  d d k S(   s$   Should the related object be hidden?iR   (   R>   (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    c         C   s   |  j  j j S(   s   
        Returns the field in the to' object to which this relationship is tied
        (this is always the primary key on the target model). Provided for
        symmetry with ManyToOneRel.
        (   R7   R   RB   (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    N(   R   Rk   RC   Rd   Ro   R   R   (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s   
	t
   ForeignKeyc           B   s   e  Z e Z i e d   d 6Z e d  Z d e d  Z	 d   Z
 d   Z d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z RS(   s.   Model %(model)s with pk %(pk)r does not exist.t   invalids.   Foreign Key (type determined by related field)c         K   s0  y | j  j j   } Wn< t k
 rT t | t  s t d |  j j | t	 f  nN X| j  j
 s t d |  j j | j  j f  | p | j  j o | j  j j } | j d d   | d <| | | d | j d d   d | j d d   d | j d d   d | j d t  | d <t j |  |  t |  _ d  S(	   Nsg   %s(%r) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string %rs2   %s cannot define a relation with abstract class %sRH   R>   R   R   R   R6   (   R   R!   R?   R    R@   RA   R   R   R   R   R=   RB   RD   Ru   RC   R/   R"   R   Ro   Rd   t   db_index(   R   R7   t   to_fieldt	   rel_classR1   t   to_name(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo   "  s    /,!c         C   s   |  j  j r d  St t |   j | |  | d  k r9 d  S|  j  j j j i | |  j  j	 6  } | j
 |  j  j  } | j   s t j |  j d i |  j  j j j d 6| d 6  n  d  S(   NR   R+   RB   (   R6   R   R:   R   t   validateRC   R7   R   R   RI   t   complex_filterR   t   existsR   t   ValidationErrort   error_messagesR   RH   (   R   R-   t   model_instancet   qs(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   8  s    %c         C   s   d |  j  S(   Ns   %s_id(   RD   (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   get_attnameE  s    c         C   s   d |  j  |  j j   j  f S(   Ns   %s__%s__exact(   RD   R6   R   (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt    get_validator_unique_lookup_typeH  s    c         C   sG   t  t |   j   } t | |  j j  rC t | |  j j   j  S| S(   sN   Here we check if the default value is an object and return the to_field if so.(	   R:   R   t   get_defaultR@   R6   R7   Re   R   R   (   R   t   field_default(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   K  s    c         C   s<   | d k s | d  k r d  S|  j j   j | d | Sd  S(   Nt    R   (   RC   R6   R   t   get_db_prep_save(   R   R-   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   R  s    c         C   s_   | sO |  j  rO |  j rO |  j   } t |  d k rL t | d d  SqO n  t j |  |  S(   Ni   i   i    (   t   blankt   choicest   get_choices_defaultt   lenR   R   t   value_to_string(   R   R   t   choice_list(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   Y  s    c         C   s   t  t |   j | |  t | |  j t |    t |  j j t	  rV |  j j } n |  j j j
 j } | d f | j
 j |  j <d  S(   Nt   o2m(   R:   R   R4   Rv   RD   R   R@   R6   R7   RA   R   t   db_tablet   duplicate_targetst   column(   R   R&   RD   t   target(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR4   e  s    c         C   s\   |  j  j   s. t | | j   t |   n  |  j  j d  k rX | j j j	 |  j  _ n  d  S(   N(
   R6   R   Rv   R|   R   RI   RC   R   RB   RD   (   R   R&   RK   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRL   n  s    c         K   sz   | j  d d   } i t j d 6|  j j j j |  j |  j j	  d 6|  j j
 d 6} | j |  t t |   j |   S(   NRt   t
   form_classt   querysett   to_field_name(   R/   RC   R   t   ModelChoiceFieldR6   R7   R   Rt   R   R   RI   R   R:   R   t	   formfield(   R   R1   Rx   t   defaults(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   v  s    
(c         C   s   |  j  j   } | j d |  S(   NR   (   R6   R   t   related_db_type(   R   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   db_type  s    N(   R   Rk   R"   t   empty_strings_allowedRG   t   default_error_messagest   descriptionRC   R   Ro   R   R   R   R   R   R   R4   RL   R   R  (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s   										
t   OneToOneFieldc           B   sA   e  Z d  Z e d  Z d d  Z d   Z d   Z d   Z	 RS(   s  
    A OneToOneField is essentially the same as a ForeignKey, with the exception
    that always carries a "unique" constraint with it and the reverse relation
    always returns the object pointed to (since there will only ever be one),
    rather than returning a list.
    s   One-to-one relationshipc         K   s-   t  | d <t t |   j | | t |  d  S(   Nt   unique(   Rd   R:   R  Ro   R   (   R   R7   R   R1   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo     s    
c         C   s    t  | | j   t |   d  S(   N(   Rv   R|   Rl   (   R   R&   RK   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRL     s    c         K   s&   |  j  j r d  St t |   j |   S(   N(   R6   R   RC   R:   R  R   (   R   R1   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    c         C   sB   t  | |  j j  r+ t | |  j |  n t | |  j |  d  S(   N(   R@   R6   R7   Rv   RD   R   (   R   Rp   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   save_form_data  s    N(
   R   Rk   t   __doc__RG   R  RC   Ro   RL   R   R  (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR    s   		c   
      C   sB  d d l  m } t } t |  j j t  r{ |  j j t k r{ |  j j } | j d  d } d   } t	 | |  | |  nl t |  j j t  r | j
 j } | } | j
 j } n6 |  j j j
 j } |  j j } | j
 j p | j
 j } d | j
 j |  j f } |  j j t k s$| | j
 j k rGd | j   } d | j   } n | j
 j j   } | j   } t d t f i |  j | j
  d	 6| d
 6| d 6| j
 j d 6| | f d 6d i | d 6| d 6d 6d i | d 6| d 6d 6 }	 t | | j f i |	 d 6| j d 6| j | d d | | 6| j | d d | | 6 S(   Ni(   t   modelsR   c         S   s(   | j  j p | j  j |  j j j  _ d  S(   N(   R   t   managedR6   R   (   R'   R+   R&   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   set_managed  s    s   %s_%ss   from_%ss   to_%st   MetaR   R
  R   R   t   unique_togethers   %(from)s-%(to)s relationshipt   fromR7   RH   s   %(from)s-%(to)s relationshipst   verbose_name_pluralRk   R>   s   %s+(   t	   django.dbR	  Rd   R@   R6   R7   RA   R   R   R.   R   R!   R
  RD   R?   t   typet   objectt   _get_m2m_db_tableR   R   Rk   R   (
   R'   t   klassR	  R
  t   to_modelR7   R  RD   t   from_t   meta(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt&   create_many_to_many_intermediary_model  sB    '	$
t   ManyToManyFieldc           B   s   e  Z e d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z RS(   s   Many-to-many relationshipc         K   s^  y0 | j  j s/ t d |  j j | j  j f  Wn< t k
 rn t | t  so t d |  j j | t	 f  n X| j
 d d   | d <t | d | j d d   d | j d d   d | j d | t	 k  d | j d d   | d <| j d	 d   |  _ | d j d  k	 r&|  j d  k s&t d
  n  t j |  |  t d  } t |  j d |  |  _ d  S(   Ns2   %s cannot define a relation with abstract class %ssl   %s(%r) is invalid. First parameter to ManyToManyField must be either a model, a model name, or the string %rRH   R>   R   R   R   R6   R   s;   Cannot specify a db_table if an intermediary model is used.sD   Hold down "Control", or "Command" on a Mac, to select more than one.t    (   R   R=   R   R   R   R!   R    R@   RA   R   Ru   RC   R   R/   R   R   R   Ro   RG   R   t	   help_text(   R   R7   R1   t   msg(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRo     s     0/	c         C   s   t  j |  d t S(   Nt   include_blank(   R   t   get_choicesR"   (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR     s    c         C   s_   |  j  j d k	 r" |  j  j j j S|  j r2 |  j St j d | j |  j f t j	 j
    Sd S(   sL   Function that can be curried to provide the m2m table name for this relations   %s_%sN(   R6   R   RC   R   R   R   t   truncate_nameRD   R   t   opst   max_name_length(   R   R;   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR    s    	c      	   C   s   d | } t  |  |  r& t |  |  Sxm |  j j j j D]Y } t  | d  r9 | j r9 | j j | j k r9 t |  | t | |   t |  |  Sq9 Wd S(   s_   Function that can be curried to provide the source accessor or DB column name for the m2m tables   _m2m_%s_cacheR6   N(	   R<   Re   R6   R   R   t   fieldsR7   R+   Rv   (   R   RK   t   attrt
   cache_attrt   f(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   _get_m2m_attr  s    
-c      	   C   s   d | } t  |  |  r& t |  |  St } x |  j j j j D] } t  | d  r? | j r? | j j | j k r? | j	 | j k r | r t
 |  | t | |   Pq t } q t
 |  | t | |   Pq? q? Wt |  |  S(   s`   Function that can be curried to provide the related accessor or DB column name for the m2m tables   _m2m_reverse_%s_cacheR6   (   R<   Re   R"   R6   R   R   R"  R7   t   parent_modelR+   Rv   Rd   (   R   RK   R#  R$  t   foundR%  (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   _get_m2m_reverse_attr  s    
-	c         C   s   |  j  j } y t t | j d   } Wn t k
 r< d SX| j j |  } t |  t |  k r g  | D] } | | k rn | ^ qn } t	 j
 t d d t |   i |  j d 6t |  d k r | d p t |  d 6  n  d S(	   s8   Validates that the value is a valid list of foreign keyst   ,Ns@   Please enter valid %(self)s IDs. The value %(value)r is invalid.sB   Please enter valid %(self)s IDs. The values %(value)r are invalid.R   i   i    R-   (   R6   R7   t   mapt   intR   R   R   t   in_bulkR   R   R   R   RH   t   tuple(   R   t
   field_datat   all_datat   modt   pkst   objectst   kt   badkeys(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   isValidIDList  s    %
c         C   s   d } | rF t  | |  j  j   } g  | D] } | j   ^ q+ } n> |  j s |  j   } t |  d k r | d d g } q n  t |  S(   NR   i   i    (   Re   RD   R   Rq   R   R   R   R   (   R   R   R   R   Rp   t   choices_list(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   '  s    "	c         C   sR  |  j  j rI |  j  j d k s6 |  j  j | j j k rI d | |  j  _ n  t t |   j | |  |  j  j	 r | j j
 r t |  |  |  j  _	 n  t | |  j t |    t |  j | j  |  _ t |  j  j	 t  r d   } t | |  |  j  j	 |  n  t |  j  j t  r#|  j  j } n |  j  j j j } | d f | j j |  j <d  S(   NR   s   %s_rel_+c         S   s   | |  j  _ d  S(   N(   R6   R   (   R'   R+   R&   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   resolve_through_modelP  s    t   m2m(   R6   R   R7   R   R!   R>   R:   R  R4   R   R=   R  Rv   RD   R   R   R  t   m2m_db_tableR@   RA   R.   R   R   R   (   R   R&   RD   R8  R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR4   5  s    6	c         C   s   |  j  j   s. t | | j   t |   n  t |  j | d  |  _ t |  j | d  |  _	 t |  j | d  |  _
 t |  j | d  |  _ d  S(   NR   RD   (   R6   R   Rv   R|   R   R   R&  t   m2m_column_nameR)  t   m2m_reverse_nameR   R   (   R   R&   RK   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRL   Z  s    c         C   s   d  S(   N(    (   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyRJ   g  s    c         C   s   t  | |  j  j   S(   s<   Returns the value of this field in the given model instance.(   Re   R   R   (   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   value_from_objectj  s    c         C   s   t  | |  j |  d  S(   N(   Rv   R   (   R   Rp   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR  n  s    c         K   s   | j  d d   } i t j d 6|  j j j j |  j |  j j	  d 6} | j
 |  | j d  d  k	 r | d } t |  r |   } n  g  | D] } | j   ^ q | d <n  t t |   j |   S(   NRt   R   R   t   initial(   R/   RC   R   t   ModelMultipleChoiceFieldR6   R7   R   Rt   R   R   R   Ru   t   callableRq   R:   R  R   (   R   R1   Rx   R   R>  t   i(    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR   q  s    
+
&c         C   s   d  S(   N(   RC   (   R   R   (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR    s    (   R   Rk   RG   R  Ro   R   R  R&  R)  R6  R   R4   RL   RJ   R=  R  R   R  (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyR    s   			
	
				%					N(<   t   django.confR    R  R   R   R   t   django.db.backendsR   R   R   R   t   django.db.models.fieldsR   R   R	   R
   R   R   t   django.db.models.relatedR   t   django.db.models.queryR   t   django.db.models.query_utilsR   t   django.utils.encodingR   t   django.utils.translationR   RG   R   R   R   t   django.utils.functionalR   t   django.coreR   t   djangoR   R   R#   R.   R2   t   class_preparedt   connectR  R3   Rl   R   R   R"   R   R   R   R   R   R   R   R  R  R  (    (    (    sL   /home/panlixing/Python_Projects/gaeseries/django/db/models/fields/related.pyt   <module>   s>   ."	7	y:ba-3n	)