B
    ©0H[>'  ã               @   sx   d Z ddlmZ ddlmZ ddlmZ ddlmZ G dd„ deƒZ	G d	d
„ d
e	ƒZ
G dd„ de
ƒZG d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.
é    )Ú
exceptions)Úcached_propertyé   )ÚBLANK_CHOICE_DASH)ÚFieldCacheMixinc               @   s  e Zd ZdZdZdZdZdZdZd1d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edd„ ƒZdd „ Zdefd!d"„Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd2d+d,„Zd3d-d.„Z d/d0„ Z!dS )4Ú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   sF   || _ || _|| _|| _|d kr$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   úkC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\db\models\fields\reverse_related.pyÚ__init__%   s    zForeignObjectRel.__init__c             C   s   |   ¡ S )N)Ú	is_hidden)r   r   r   r   Úhidden6   s    zForeignObjectRel.hiddenc             C   s
   | j  ¡ S )N)r   r   )r   r   r   r   Úname:   s    zForeignObjectRel.namec             C   s   | j S )N)r   )r   r   r   r   Úremote_field>   s    zForeignObjectRel.remote_fieldc             C   s,   |   ¡ d j}t|ƒdkr$t d¡‚|d S )z�
        When filtering against this relation, return 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_fieldB   s    
zForeignObjectRel.target_fieldc             C   s   | j jstdƒ‚| 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_modelM   s    zForeignObjectRel.related_modelc             C   s   | j jS )N)r   Úmany_to_many)r   r   r   r   r!   T   s    zForeignObjectRel.many_to_manyc             C   s   | j jS )N)r   Úone_to_many)r   r   r   r   Úmany_to_oneX   s    zForeignObjectRel.many_to_onec             C   s   | j jS )N)r   r#   )r   r   r   r   r"   \   s    zForeignObjectRel.one_to_manyc             C   s   | j jS )N)r   Ú
one_to_one)r   r   r   r   r$   `   s    zForeignObjectRel.one_to_onec             C   s   | j  |¡S )N)r   Ú
get_lookup)r   Úlookup_namer   r   r   r%   d   s    zForeignObjectRel.get_lookupc             C   s
   | j  ¡ S )N)r   Úget_internal_type)r   r   r   r   r'   g   s    z"ForeignObjectRel.get_internal_typec             C   s   | j jS )N)r   Údb_type)r   r   r   r   r(   j   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    Ú_metaZ	app_labelÚ
model_name)r   r   r   r   Ú__repr__n   s    zForeignObjectRel.__repr__c             C   s"   |r|ng dd„ | j j ¡ D ƒ S )a  
        Return choices with a default blank choices included, for use
        as <select> choices for this field.

        Analog of django.db.models.fields.Field.get_choices(), provided
        initially for utilization by RelatedFieldListFilter.
        c             S   s   g | ]}|j t|ƒf‘qS r   )ÚpkÚstr)Ú.0Úxr   r   r   ú
<listcomp>~   s    z0ForeignObjectRel.get_choices.<locals>.<listcomp>)r    Z_default_managerÚall)r   Zinclude_blankZblank_choicer   r   r   Úget_choicesu   s    
zForeignObjectRel.get_choicesc             C   s   t | jƒo| jd dkS )z$Should the related object be hidden?r   ú+)Úboolr
   )r   r   r   r   r   �   s    zForeignObjectRel.is_hiddenc             C   s
   | 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  |||¡S )N)r   Úget_extra_restriction)r   Zwhere_classÚaliasZrelated_aliasr   r   r   r8   ˆ   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   sV   |r
|j n| jj }|p| j}| jr6| jr6|| jkr6d S | jrB| jS |j| jrPdnd S )NZ_setÚ )r+   r    r   r   r	   r
   r,   )r   r	   Úoptsr   r   r   Úget_accessor_name•   s    
z"ForeignObjectRel.get_accessor_namec             C   s   | j  |¡S )N)r   Zget_reverse_path_info)r   Zfiltered_relationr   r   r   r   ¦   s    zForeignObjectRel.get_path_infoc             C   s   |   ¡ S )z„
        Return the name of the cache key to use for storing an instance of the
        forward model on the reverse model.
        )r>   )r   r   r   r   Úget_cache_name©   s    zForeignObjectRel.get_cache_name)NNNFN)N)N)"r*   Ú
__module__Ú__qualname__Ú__doc__Zauto_createdÚconcreteÚeditableZis_relationÚnullr   r   r   r   Úpropertyr   r   r    r!   r#   r"   r$   r%   r'   r(   r-   r   r4   r   r7   r8   r;   r>   r   r?   r   r   r   r   r      s:    



r   c                   s:   e Zd ZdZd‡ fdd„	Zdd„ Zdd	„ Zd
d„ Z‡  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	       	   	      s$   t ƒ j|||||||d� || _d S )N)r
   r   r   r   r   )Úsuperr   r:   )	r   r   r   r:   r
   r   r   r   r   )Ú	__class__r   r   r   À   s    zManyToOneRel.__init__c             C   s   | j  ¡ }| dd ¡ |S )Nr    )Ú__dict__ÚcopyÚpop)r   Ústater   r   r   Ú__getstate__Í   s    
zManyToOneRel.__getstate__c             C   s*   | j j | j¡}|js&t d| j ¡‚|S )zY
        Return the Field in the 'to' object to which this relationship is tied.
        zNo related field named '%s')r	   r+   Ú	get_fieldr:   rC   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)r:   r	   r+   r.   r   )r   r   r   r   r;   Û   s    zManyToOneRel.set_field_name)NNNFN)	r*   r@   rA   rB   r   rN   rP   r;   Ú__classcell__r   r   )rI   r   rG   ±   s    	rG   c                   s"   e Zd ZdZd‡ fdd„	Z‡  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	       	   
      s&   t ƒ j||||||||d� d| _d S )N)r
   r   r   r   r   F)rH   r   r   )	r   r   r   r:   r
   r   r   r   r   )rI   r   r   r   ç   s    zOneToOneRel.__init__)NNNFN)r*   r@   rA   rB   r   rQ   r   r   )rI   r   rR   ß   s    rR   c                   s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  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
       
         sR   t ƒ j|||||d� |r&|	s&tdƒ‚|| _|r<|s<tdƒ‚|| _|| _|	| _d S )N)r
   r   r   z4Can't supply a through model and db_constraint=Falsez5Cannot specify through_fields without a through model)rH   r   Ú
ValueErrorÚthroughÚthrough_fieldsr   Údb_constraint)
r   r   r   r
   r   r   r   rU   rV   rW   )rI   r   r   r   ý   s    zManyToManyRel.__init__c             C   sX   | j j}| jr | | jd ¡}n.x,|jD ]"}t|ddƒ}|r(|j| jkr(P q(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)rU   r+   rV   rO   ÚfieldsÚgetattrr	   Zforeign_related_fields)r   r=   r   Úrelr   r   r   rP     s    zManyToManyRel.get_related_field)NNNTNNT)r*   r@   rA   rB   r   rP   rQ   r   r   )rI   r   rS   õ   s
     rS   N)rB   Zdjango.corer   Zdjango.utils.functionalr   r<   r   Zmixinsr   r   rG   rR   rS   r   r   r   r   Ú<module>
   s    .