3
>^}                 @   s   d Z ddlmZmZ eddddddd	gZed
ddddddgZdd Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd  Zd!d" Zd#S )$z
Helper function for returning the field information that is associated
with a model class. This includes returning all the forward and reverse
relationships and their associated metadata.

Usage: `get_field_info(model)` returns a `FieldInfo` instance.
    )OrderedDict
namedtupleZFieldResultpkfieldsforward_relationsreverse_relationsfields_and_pkZ	relationsRelationInfomodel_fieldrelated_modelto_manyto_fieldhas_through_modelreversec             C   sP   | j jj }t|}t|}t|}t|}t||}t||}t||||||S )z
    Given a model class, returns a `FieldInfo` instance, which is a
    `namedtuple`, containing metadata about the various field types on the model
    including information about their relationships.
    )	_metaZconcrete_model_get_pk_get_fields_get_forward_relationships_get_reverse_relationships_merge_fields_and_pk_merge_relationships	FieldInfo)modeloptsr   r   r   r   r   Zrelationships r   N/tmp/pip-build-8app2_gc/djangorestframework/rest_framework/utils/model_meta.pyget_field_info   s    



r   c             C   s2   | j }|j}x |r,|jr,|jjjj }|j}qW |S )N)r   remote_fieldZparent_linkr   r   )r   r   relr   r   r   r   0   s    
r   c             C   s.   t  }x"dd | jD D ]}|||j< qW |S )Nc             S   s   g | ]}|j r|j r|qS r   )	serializer   ).0fieldr   r   r   
<listcomp>>   s    z_get_fields.<locals>.<listcomp>)r   r   name)r   r   r!   r   r   r   r   <   s    r   c             C   s   t | dd o| jd S )N	to_fieldsr   )getattrr$   )r!   r   r   r   _get_to_fieldD   s    r&   c          	   C   s   t  }x:dd | jD D ]&}t||jjdt|ddd||j< qW x@dd | jD D ],}t||jjdd|jjj	j
 dd||j< qTW |S )zD
    Returns an `OrderedDict` of field names to `RelationInfo`.
    c             S   s   g | ]}|j r|jr|qS r   )r   r   )r    r!   r   r   r   r"   M   s    z._get_forward_relationships.<locals>.<listcomp>F)r
   r   r   r   r   r   c             S   s   g | ]}|j r|qS r   )r   )r    r!   r   r   r   r"   X   s    TN)r   r   r	   r   r   r&   r#   many_to_manythroughr   auto_created)r   r   r!   r   r   r   r   H   s$    r   c          
   C   s   t  }dd | jD }x:|D ]2}|j }td|j|jjjt|jddd||< qW dd | jD }xN|D ]F}|j }td|jddt	|jjdddk	o|jjj
jj dd||< qhW |S )	zD
    Returns an `OrderedDict` of field names to `RelationInfo`.
    c             S   s   g | ]}|j js|qS r   )r!   r'   )r    rr   r   r   r"   m   s    z._get_reverse_relationships.<locals>.<listcomp>NFT)r
   r   r   r   r   r   c             S   s   g | ]}|j jr|qS r   )r!   r'   )r    r*   r   r   r   r"   z   s    r(   )r   Zrelated_objectsZget_accessor_namer	   r   r!   r   multipler&   r%   r(   r   r)   )r   r   Zall_related_objectsZrelationZaccessor_nameZ all_related_many_to_many_objectsr   r   r   r   h   s.    

r   c             C   s&   t  }| |d< | || j< |j| |S )Nr   )r   r#   update)r   r   r   r   r   r   r      s
    

r   c             C   s   t t| j t|j  S )N)r   listitems)r   r   r   r   r   r      s    
r   c             C   s   t | dot | jdo| jjS )z_
    Given a model class, returns a boolean True if it is abstract and False if it is not.
    r   abstract)hasattrr   r/   )r   r   r   r   is_abstract_model   s    r1   N)__doc__collectionsr   r   r   r	   r   r   r   r&   r   r   r   r   r1   r   r   r   r   <module>   s0    %	