

_V+                 @   s   d  Z  d d l m Z d d l 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 Gd
 d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z d S)al  
"Rel objects" for related fields.

"Rel objects" (for lack of a better name) carry information about the relation
modeled by a related field and provide some utility functions. They're stored
in the ``remote_field`` attribute of the field.

They also act as reverse fields for the purposes of the Meta API because
they're the closest concept currently available.
    )unicode_literalsN)
exceptions)RemovedInDjango20Warning)
smart_text)cached_property   )BLANK_CHOICE_DASHc               @   s  e  Z d  Z d Z d Z d Z d Z d Z d Z d d d d d d d  Z	 e
 d d    Z e d	 d
    Z e d d    Z e
 d d    Z e
 d d    Z e d d    Z e d d    Z e d d    Z e d d    Z e d d    Z d d   Z d d   Z d d    Z e
 d! d"    Z d# d$   Z d e d d% d&  Z d d' d(  Z d) d*   Z d+ d,   Z d- d.   Z  d/ d0   Z! d d1 d2  Z" d3 d4   Z# d5 d6   Z$ d S)7ForeignObjectRelz
    Used by ForeignObject to store information about the relation.

    ``_meta.get_fields()`` returns this class to provide access to the field
    flags for the reverse relation.
    TFNc             C   sg   | |  _  | |  _ | |  _ | |  _ | d  k r6 i  n | |  _ | |  _ | |  _ d |  _ d |  _ d  S)NFT)	fieldmodelrelated_namerelated_query_namelimit_choices_toparent_link	on_deletesymmetricalmultiple)selfr
   tor   r   r   r   r    r   I/tmp/pip-build-ghmbqnp_/Django/django/db/models/fields/reverse_related.py__init__*   s    							zForeignObjectRel.__init__c             C   s   t  j d t d  |  j S)Nz\Usage of ForeignObjectRel.to attribute has been deprecated. Use the model attribute instead.   )warningswarnr   r   )r   r   r   r   r   ;   s    
zForeignObjectRel.toc             C   s
   |  j    S)N)	is_hidden)r   r   r   r   hiddenC   s    zForeignObjectRel.hiddenc             C   s   |  j  j   S)N)r
   r   )r   r   r   r   nameG   s    zForeignObjectRel.namec             C   s   |  j  S)N)r
   )r   r   r   r   remote_fieldK   s    zForeignObjectRel.remote_fieldc             C   s?   |  j    d j } t |  d k r7 t j d   n  | d S)z
        When filtering against this relation, returns the field on the remote
        model against which the filtering should happen.
        r   z1Can't use target_field for multicolumn relations.r   )get_path_infotarget_fieldslenr   Z
FieldError)r   r!   r   r   r   target_fieldO   s    zForeignObjectRel.target_fieldc             C   s%   |  j  j s t d   n  |  j  j S)NzVThis property can't be accessed before self.field.contribute_to_class has been called.)r
   r   AttributeError)r   r   r   r   related_modelZ   s    zForeignObjectRel.related_modelc             C   s
   |  j  j S)N)r
   many_to_many)r   r   r   r   r&   a   s    zForeignObjectRel.many_to_manyc             C   s
   |  j  j S)N)r
   one_to_many)r   r   r   r   many_to_onee   s    zForeignObjectRel.many_to_onec             C   s
   |  j  j S)N)r
   r(   )r   r   r   r   r'   i   s    zForeignObjectRel.one_to_manyc             C   s
   |  j  j S)N)r
   
one_to_one)r   r   r   r   r)   m   s    zForeignObjectRel.one_to_onec             C   s   |  j  j | |  S)N)r
   get_prep_lookup)r   lookup_namevaluer   r   r   r*   q   s    z ForeignObjectRel.get_prep_lookupc             C   s   |  j  j |  S)N)r
   
get_lookup)r   r+   r   r   r   r-   t   s    zForeignObjectRel.get_lookupc             C   s   |  j  j   S)N)r
   get_internal_type)r   r   r   r   r.   w   s    z"ForeignObjectRel.get_internal_typec             C   s
   |  j  j S)N)r
   db_type)r   r   r   r   r/   z   s    zForeignObjectRel.db_typec             C   s,   d t  |   j |  j j j |  j j j f S)Nz<%s: %s.%s>)type__name__r%   _meta	app_label
model_name)r   r   r   r   __repr__~   s    zForeignObjectRel.__repr__c             C   sk   | r | n g  } |  j  j j   } | rP | j i d d |  j  j j 6 } n  d d   | D } | | S)a  
        Return choices with a default blank choices included, for use as
        SelectField choices for this field.

        Analog of django.db.models.fields.Field.get_choices(), provided
        initially for utilization by RelatedFieldListFilter.
        Fz
%s__isnullc             S   s(   g  |  ] } | j    t |  f  q Sr   )Z_get_pk_valr   ).0xr   r   r   
<listcomp>   s   	 z0ForeignObjectRel.get_choices.<locals>.<listcomp>)r%   Z_default_managerallZcomplex_filterr2   r4   )r   Zinclude_blankZblank_choiceZlimit_to_currently_relatedZfirst_choiceZquerysetlstr   r   r   get_choices   s    	 zForeignObjectRel.get_choicesc             C   s   |  j  j | | d | d | S)N
connectionprepared)r
   get_db_prep_lookup)r   Zlookup_typer,   r<   r=   r   r   r   r>      s    z#ForeignObjectRel.get_db_prep_lookupc             C   s    t  |  j  o |  j d d k S)z$Should the related object be hidden?r   +r   )boolr   )r   r   r   r   r      s    zForeignObjectRel.is_hiddenc             C   s   |  j  j   S)N)r
   Zget_reverse_joining_columns)r   r   r   r   get_joining_columns   s    z$ForeignObjectRel.get_joining_columnsc             C   s   |  j  j | | |  S)N)r
   get_extra_restriction)r   Zwhere_classaliasZrelated_aliasr   r   r   rB      s    z&ForeignObjectRel.get_extra_restrictionc             C   s   d |  _  d S)z
        Set the related field's name, this is not available until later stages
        of app loading, so set_field_name is called from
        set_attributes_from_rel()
        N)
field_name)r   r   r   r   set_field_name   s    zForeignObjectRel.set_field_namec             C   s   | r | j  n	 |  j j  } | p' |  j } |  j rR |  j rR | |  j k rR d  Sn  |  j rb |  j S| j r | j i | j j   d 6| j	 j   d 6S| j |  j r d n d S)Nr4   r3   Z_set )
r2   r%   r   r   r   r   Zdefault_related_namer4   lowerr3   )r   r   optsr   r   r   get_accessor_name   s    				z"ForeignObjectRel.get_accessor_namec             C   s   d |  j    S)Nz	_%s_cache)rI   )r   r   r   r   get_cache_name   s    zForeignObjectRel.get_cache_namec             C   s   |  j  j   S)N)r
   Zget_reverse_path_info)r   r   r   r   r       s    zForeignObjectRel.get_path_info)%r1   
__module____qualname____doc__Zauto_createdconcreteeditableZis_relationnullr   propertyr   r   r   r   r   r#   r%   r&   r(   r'   r)   r*   r-   r.   r/   r5   r   r;   r>   r   rA   rB   rE   rI   rJ   r    r   r   r   r   r	      sB   
r	   c                   s[   e  Z d  Z d Z d d d d d   f d d  Z d d   Z d d	   Z d
 d   Z   S)ManyToOneRela+  
    Used by the ForeignKey field to store information about the relation.

    ``_meta.get_fields()`` returns this class to provide access to the field
    flags for the reverse relation.

    Note: Because we somewhat abuse the Rel objects by using them as reverse
    fields we get the funny situation where
    ``ManyToOneRel.many_to_one == False`` and
    ``ManyToOneRel.one_to_many == True``. This is unfortunate but the actual
    ManyToOneRel class is a private API and there is work underway to turn
    reverse relations into actual fields.
    NFc	       	         sD   t  t |   j | | d | d | d | d | d | | |  _ d  S)Nr   r   r   r   r   )superrR   r   rD   )	r   r
   r   rD   r   r   r   r   r   )	__class__r   r   r      s    	zManyToOneRel.__init__c             C   s#   |  j  j   } | j d d   | S)Nr%   )__dict__copypop)r   stater   r   r   __getstate__   s    zManyToOneRel.__getstate__c             C   s>   |  j  j j |  j  } | j s: t j d |  j   n  | S)zY
        Return the Field in the 'to' object to which this relationship is tied.
        zNo related field named '%s')r   r2   	get_fieldrD   rN   r   ZFieldDoesNotExist)r   r
   r   r   r   get_related_field   s
    		zManyToOneRel.get_related_fieldc             C   s"   |  j  p |  j j j j |  _  d  S)N)rD   r   r2   pkr   )r   r   r   r   rE      s    zManyToOneRel.set_field_name)r1   rK   rL   rM   r   rY   r[   rE   r   r   )rT   r   rR      s   
rR   c                   s7   e  Z d  Z d Z d d d d d   f d d  Z   S)OneToOneRelz
    Used by OneToOneField to store information about the relation.

    ``_meta.get_fields()`` returns this class to provide access to the field
    flags for the reverse relation.
    NFc	       	         sG   t  t |   j | | | d | d | d | d | d | d |  _ d  S)Nr   r   r   r   r   F)rS   r]   r   r   )	r   r
   r   rD   r   r   r   r   r   )rT   r   r   r     s    zOneToOneRel.__init__)r1   rK   rL   rM   r   r   r   )rT   r   r]      s   r]   c            
       sI   e  Z d  Z d Z d d d d d d d   f d d  Z d d   Z   S)ManyToManyRelz
    Used by ManyToManyField to store information about the relation.

    ``_meta.get_fields()`` returns this class to provide access to the field
    flags for the reverse relation.
    NTc
       
   	      s   t  t |   j | | d | d | d | | rG |	 rG t d   n  | |  _ | rl | rl t d   n  | |  _ | |  _ |	 |  _ d  S)Nr   r   r   z4Can't supply a through model and db_constraint=Falsez5Cannot specify through_fields without a through model)rS   r^   r   
ValueErrorthroughthrough_fieldsr   db_constraint)
r   r
   r   r   r   r   r   r`   ra   rb   )rT   r   r   r     s    				zManyToManyRel.__init__c             C   s{   |  j  j } |  j r. | j |  j d  } nB x? | j D]4 } t | d d  } | r8 | j |  j k r8 Pq8 q8 W| j d S)z
        Return the field in the 'to' object to which this relationship is tied.
        Provided for symmetry with ManyToOneRel.
        r   r   N)r`   r2   ra   rZ   fieldsgetattrr   Zforeign_related_fields)r   rH   r
   relr   r   r   r[   .  s    	zManyToManyRel.get_related_field)r1   rK   rL   rM   r   r[   r   r   )rT   r   r^     s
   r^   )rM   
__future__r   r   Zdjango.corer   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   Zdjango.utils.functionalr   rF   r   objectr	   rR   r]   r^   r   r   r   r   <module>
   s   /