3

\                 @   sL   d dl mZ dd Zdd Zdd Zdd	 ZdddZG dd dejZd
S )    )modelsc             C   s   | j jS )N)_metadb_table)model r   ./var/www/html/oscar2019/apps/order/managers.py
_get_table   s    r   c             C   s   dt | | jj|jf S )Nz%s.%s)r   r   	get_fieldcolumn)r   attrr   r   r   _get_column
   s    r   c             C   s"   | j j|j}dt|j|jf S )Nz%s.%s)r   r	   relr   to
field_name)r   r   relatedr   r   r   _get_related_column   s    r   c             C   s   dt | t| |t| |f S )Nz1SELECT COUNT(*) FROM %s WHERE %s = %s
           )r   r   r   )r   r   r   r   r   _related_count_sql   s    r   Nc       	      C   s   t |tjjr|jj }|dkr.|jjjd }|dkrlx4|jjj	D ]&}t |tj
jrB|jj| jkrB|j}qBW t|j|}|jj|jj\}}}|r|d|d  7 }| j||i|dS )a  
    Count the rows matching `query` related to this model by their
    foreign key attribute `related_attr`, and store the result in
    `count_attr`.

    If `count_attr` is None, use the name of the module given in `query`
    suffixed with '__count'.

    If `related_attr` is None, find the first foreign key field in the
    model queried by `query` relating to this model.

    If `query` is a model class, use the all() method on its default
    manager as the query.
    N__countz AND %sr   )selectparams)
isinstancer   base	ModelBase_default_managerallr   r   module_namefieldsr   RelatedFieldr   r   namer   _filtersget_sqlextra)	selfquery
count_attrrelated_attrfieldselect_countjoinswheresr   r   r   r   _count_related   s    

r*   c               @   s   e Zd ZdddZdS )OrderQuerySetNc       	      C   s   t |tjjr|jj }|dkr.|jjjd }|dkrlx4|jjj	D ]&}t |tj
jrB|jj| jkrB|j}qBW t|j|}|jj|jj\}}}|r|d|d  7 }| j||i|dS )a  
        Count the rows matching `query` related to this model by their
        foreign key attribute `related_attr`, and store the result in
        `count_attr`.

        If `count_attr` is None, use the name of the module given in `query`
        suffixed with '__count'.

        If `related_attr` is None, find the first foreign key field in the
        model queried by `query` relating to this model.

        If `query` is a model class, use the all() method on its default
        manager as the query.
        Nr   z AND %sr   )r   r   )r   r   r   r   r   r   r   r   
model_namer   r   r   r   r   r   r   r   r    r!   )	r"   r#   r$   r%   r&   r'   r(   r)   r   r   r   r   count_related<   s    

zOrderQuerySet.count_related)NN)__name__
__module____qualname__r-   r   r   r   r   r+   :   s   r+   )NN)		django.dbr   r   r   r   r   r*   QuerySetr+   r   r   r   r   <module>   s   
