
TQc           @   s  d  Z  d d l Z d d l Z d d l Z d d l 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 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 Z e Z d Z e j  Z  d e! f d     YZ" d e" f d     YZ# d e# f d     YZ$ d e" f d     YZ% d e" f d     YZ& d d e' e' e( d  Z) d d  Z* d e! f d     YZ+ e( e( e' d  Z, d   Z- d   Z. d    Z/ d S(!   sM   
The main QuerySet implementation. This provides the public API for the ORM.
iN(   t
   exceptions(   t   connectionst   routert   transactiont   IntegrityError(   t
   LOOKUP_SEP(   t	   AutoField(   t   Qt   select_related_descendt   deferred_class_factoryt   InvalidQuery(   t	   Collector(   t   sql(   t	   partition(   t   sixid   i   t   QuerySetc           B   s  e  Z d  Z d> d> d> 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 d   Z d   Z d   Z d> d  Z d   Z d> d  Z d   Z d   Z e e _ d   Z e e _ d   Z e e _ d   Z e e _ d   Z  d   Z! d   Z" d   Z# d 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,   Z0 d-   Z1 d> d> d> d> d> d> d.  Z2 d/   Z3 d0   Z4 d1   Z5 d2   Z6 d3   Z7 e8 e7  Z7 e8 d4    Z9 d5   Z: d> e; d6  Z< d> d7  Z= d8   Z> d9   Z? d:   Z@ d;   ZA d<   ZB d=   ZC e ZD RS(?   sA   
    Represents a lazy database lookup for a set of objects.
    c         C   sp   | |  _  | |  _ | p' t j |  j   |  _ d  |  _ d  |  _ t |  _	 t |  _
 g  |  _ t |  _ i  |  _ d  S(   N(   t   modelt   _dbR   t   Queryt   queryt   Nonet   _result_cachet   _itert   Falset   _sticky_filtert
   _for_writet   _prefetch_related_lookupst   _prefetch_donet   _known_related_objects(   t   selfR   R   t   using(    (    s/   ../Django//lib/python/django/db/models/query.pyt   __init__$   s    								c         C   se   |  j    } xR |  j j   D]A \ } } | d k rD d | j | <q t j | |  | j | <q W| S(   sD   
        Deep copy of a QuerySet doesn't populate the cache
        R   R   (   s   _iters   _result_cacheN(   t	   __class__t   __dict__t   itemsR   t   copyt   deepcopy(   R   t   memot   objt   kt   v(    (    s/   ../Django//lib/python/django/db/models/query.pyt   __deepcopy__5   s    c         C   s'   t  |   |  j j   } d | d <| S(   s4   
        Allows the QuerySet to be pickled.
        R   N(   t   lenR!   R#   R   (   R   t   obj_dict(    (    s/   ../Django//lib/python/django/db/models/query.pyt   __getstate__A   s    

c         C   s=   t  |  t d   } t |  t k r3 d | d <n  t |  S(   Ni   s$   ...(remaining elements truncated)...i(   t   listt   REPR_OUTPUT_SIZER*   t   repr(   R   t   data(    (    s/   ../Django//lib/python/django/db/models/query.pyt   __repr__L   s    c         C   s   |  j  d  k rE |  j r- t |  j  |  _  qd t |  j    |  _  n |  j rd |  j  j |  j  n  |  j r |  j r |  j   n  t	 |  j   S(   N(
   R   R   R   R-   t   iteratort   extendR   R   t   _prefetch_related_objectsR*   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   __len__R   s    		c         C   sj   |  j  r  |  j r  t |   n  |  j d  k rJ |  j   |  _ g  |  _ n  |  j r] |  j   St |  j  S(   N(	   R   R   R*   R   R   R2   R   t   _result_itert   iter(   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   __iter__a   s    	
c         c   s|   d } xo t  |  j  } x& | | k  r@ |  j | V| d } q W|  j sS t  n  t  |  j  | k r	 |  j   q	 q	 d  S(   Ni    i   (   R*   R   R   t   StopIterationt   _fill_cache(   R   t   post   upper(    (    s/   ../Django//lib/python/django/db/models/query.pyR6   q   s    		c         C   si   |  j  r  |  j r  t |   n  |  j d  k	 r< t |  j  Sy t t |    Wn t k
 rd t	 SXt
 S(   N(   R   R   R*   R   R   t   boolt   nextR7   R9   R   t   True(   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   __bool__}   s    c         C   s   t  |   j |   S(   N(   t   typeR@   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   __nonzero__   s    c         C   s   d } |  j  d  k	 rM | |  j  k r( t S|  j d  k r; t St |  j   } n t |   } xf t r t |  j   | k r |  j d d  n  |  j d  k r t S|  j  | | k r t S| d 7} q\ Wd  S(   Ni    t   numi   (   R   R   R?   R   R   R*   R7   R:   (   R   t   valR;   t   it(    (    s/   ../Django//lib/python/django/db/models/query.pyt   __contains__   s     	c         C   s?  t  | t f t j  s" t  n  t  | t  r> | d k s t  | t  r | j d k sk | j d k r | j d k s | j d k s t d   |  j	 d k	 r6|  j
 d k	 r+t  | t  r | j d k	 r t | j  } q d } n
 | d } t |  j	  | k  r+|  j | t |  j	   q+n  |  j	 | St  | t  r|  j   } | j d k	 rrt | j  } n d } | j d k	 rt | j  } n d } | j j | |  | j rt |  d d | j  p| Sy5 |  j   } | j j | | d  t |  d SWn( |  j j k
 r:} t | j   n Xd S(   sE   
        Retrieves an item or slice from the set of results.
        i    s#   Negative indexing is not supported.i   N(   t
   isinstancet   sliceR   t   integer_typest	   TypeErrort   startR   t   stopt   AssertionErrorR   R   t   intR*   R:   t   _cloneR   t
   set_limitst   stepR-   R   t   DoesNotExistt
   IndexErrort   args(   R   R'   t   boundt   qsRK   RL   t   e(    (    s/   ../Django//lib/python/django/db/models/query.pyt   __getitem__   s@    	-!		
 )c         C   s\   |  j  |  t | t  r& | j   S|  j   } | j |  | j j | j t j  | S(   N(	   t   _merge_sanity_checkRG   t   EmptyQuerySetRO   t   _merge_known_related_objectsR   t   combineR   t   AND(   R   t   othert   combined(    (    s/   ../Django//lib/python/django/db/models/query.pyt   __and__   s    
c         C   sV   |  j  |  |  j   } t | t  r, | S| j |  | j j | j t j  | S(   N(	   RY   RO   RG   RZ   R[   R   R\   R   t   OR(   R   R^   R_   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   __or__   s    c         c   s  t  } t |  j j j r( |  j j } n  t | t  r@ | } n d } |  j j
 } t |  j j  } t |  j j  } |  j j   } | s |  j j j } n  g  } | r5x |  j j j   D]r \ }	 }
 |
 d k r |  j }
 n  y* |	 j | |
 k r	| j |	 j  n  Wq t k
 r-| j |	 j  q Xq Wn  t |  } | t | pY|  j j j  } d } | r| rt   } g  } x@ | D]8 }	 |	 j | k r| j |	 j  q| j |	 j  qWt |  j |  } n  |  j } |  j }
 |  j j d |  } | r(t |
 d | d | d | } n  x| j   D]~} | rkt | | | | d t |  \ } } nU | | | !} | r| t t | |     } n |
 |   } | | j  _ t  | j  _! | rx1 t" |  D]  \ } } t# | | | |  qWn  | r8x5 t" |  D]$ \ } } t# | | | | |  qWn  |  j$ rxj |  j$ j%   D]V \ }	 } t& | |	 j'    } y | | } Wn t k
 rqQXt# | |	 j |  qQWn  | Vq5Wd S(   sc   
        An iterator over the results from applying this QuerySet to the
        database.
        R   t	   max_deptht	   requestedt	   only_loadt   offsetN((   R   R   t   dbt   featurest   supports_select_relatedR   t   select_relatedRG   t   dictR   Rc   R-   t   extra_selectt   aggregate_selectt   get_loaded_field_namesR   t   _metat   fieldst   get_fields_with_modelt   namet   appendt   KeyErrorR*   t   sett   addt   attnameR	   t   get_compilert   get_klass_infot   results_itert   get_cached_rowt   zipt   _statet   addingt	   enumeratet   setattrR   R"   t   getattrt   get_attname(   R   t
   fill_cacheRd   Rc   Rl   Rm   Re   Rp   t   load_fieldst   fieldR   t   index_startt   aggregate_startt   skipt	   init_listt	   model_clsRg   t   compilert
   klass_infot   rowR&   t   _t   row_datat   iR'   t	   aggregatet   rel_objst   pkt   rel_obj(    (    s/   ../Django//lib/python/django/db/models/query.pyR2      s    					c         O   s   |  j  j r t d   n  x | D] } | | | j <q" W|  j  j   } x6 | j   D]( \ } } | j | |  j | d t qU W| j	 d |  j
  S(   s   
        Returns a dictionary containing the calculations (aggregation)
        over the current queryset

        If args is present the expression is passed as a kwarg using
        the Aggregate object's default alias.
        s/   aggregate() + distinct(fields) not implemented.t
   is_summaryR   (   R   t   distinct_fieldst   NotImplementedErrort   default_aliast   cloneR"   t   add_aggregateR   R?   t   get_aggregationRg   (   R   RT   t   kwargst   argR   t   aliast   aggregate_expr(    (    s/   ../Django//lib/python/django/db/models/query.pyR   T  s    c         C   s<   |  j  d k	 r& |  j r& t |  j   S|  j j d |  j  S(   s   
        Performs a SELECT COUNT() and returns the number of records as an
        integer.

        If the QuerySet is already fully cached this simply returns the length
        of the cached results set to avoid multiple SELECT COUNT(*) calls.
        R   N(   R   R   R   R*   R   t	   get_countRg   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   counti  s    c         O   s   |  j  | |   } |  j j   r0 | j   } n  t |  } | d k rS | j d S| s |  j j d |  j j j	 | f   n  |  j j
 d |  j j j	 | | f   d S(   sn   
        Performs the query and returns a single object matching the given
        keyword arguments.
        i   i    s;   %s matching query does not exist. Lookup parameters were %ssL   get() returned more than one %s -- it returned %s! Lookup parameters were %sN(   t   filterR   t
   can_filtert   order_byR*   R   R   RR   Ro   t   object_namet   MultipleObjectsReturned(   R   RT   R   R   RC   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   getv  s    		c         K   s5   |  j  |   } t |  _ | j d t d |  j  | S(   s   
        Creates a new object with the given kwargs, saving it to the database
        and returning the created object.
        t   force_insertR   (   R   R?   R   t   saveRg   (   R   R   R&   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   create  s    	c   	      C   s  | d k s | d k s t  |  j j j r< t d   n  | sF | St |  _ t |  j	 } |  j j j
 } t j d |  j	  s t j d |  j	  t } n t } z | j j r |  j j j r |  j | | |  nx t d   |  \ } } | r
|  j | | |  n  | rNg  | D] } t | t  s| ^ q} |  j | | |  n  | rjt j d |  j	  n t j d |  j	  Wd | rt j d |  j	  n  X| S(   s  
        Inserts each of the instances into the database. This does *not* call
        save() on each of the instances, does not send any pre/post save
        signals, and does not set the primary key attribute if it is an
        autoincrement field.
        i    s$   Can't bulk create an inherited modelR   c         S   s   |  j  d  k S(   N(   R   R   (   t   o(    (    s/   ../Django//lib/python/django/db/models/query.pyt   <lambda>  s    N(   R   RM   R   Ro   t   parentst
   ValueErrorR?   R   R   Rg   t   local_fieldsR   t
   is_managedt   enter_transaction_managementR   Rh   t6   can_combine_inserts_with_and_without_auto_increment_pkt   has_auto_fieldt   _batched_insertR   RG   R   t   committ   commit_unless_managedt   leave_transaction_management(	   R   t   objst
   batch_sizet
   connectionRp   t   forced_managedt   objs_with_pkt   objs_without_pkt   f(    (    s/   ../Django//lib/python/django/db/models/query.pyt   bulk_create  s8    		(c         K   s  | s t  d   | j d i   } | j   } xB |  j j j D]1 } | j | k r@ | j | j  | | j <q@ q@ Wy  t |  _	 |  j
 |   t f SWn8|  j j k
 ry t g  | j   D]$ \ } } d | k r | | f ^ q  } | j |  |  j |   } t j d |  j  }	 | j d t d |  j  t j |	 d |  j | t f SWqt k
 r}
 t j |	 d |  j t j   } y |  j
 |   t f SWq|  j j k
 rt j |   qXqXn Xd S(   s   
        Looks up an object with the given kwargs, creating one if necessary.
        Returns a tuple of (object, created), where created is a boolean
        specifying whether an object was created.
        s<   get_or_create() must be passed at least one keyword argumentt   defaultst   __R   R   N(   RM   t   popR#   R   Ro   Rp   Rw   Rr   R?   R   R   R   RR   Rk   R"   t   updateR   t	   savepointRg   R   t   savepoint_commitR   t   savepoint_rollbackt   syst   exc_infoR   t   reraise(   R   R   R   t   lookupR   R'   R(   t   paramsR&   t   sidRW   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   get_or_create  s4    		 	=c         C   s   | p |  j  j j } t |  s- t d   |  j j   sH t d   |  j   } | j j d d  | j j	   | j j
 d |  | j   S(   s   
        Returns the latest object, according to the model's 'get_latest_by'
        option or optional given field_name.
        sO   latest() requires either a field_name parameter or 'get_latest_by' in the models2   Cannot change a query once a slice has been taken.t   highi   s   -%s(   R   Ro   t   get_latest_byR=   RM   R   R   RO   RP   t   clear_orderingt   add_orderingR   (   R   t
   field_namet	   latest_byR&   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   latest  s    	c         C   sf   |  j  j   s t d   | s% i  S|  j d |  j   } t g  | D] } | j   | f ^ qG  S(   sh   
        Returns a dictionary mapping each of the given IDs to the object with
        that ID.
        s+   Cannot use 'limit' or 'offset' with in_bulkt   pk__in(   R   R   RM   R   R   Rk   t   _get_pk_val(   R   t   id_listRV   R&   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   in_bulk  s    	c         C   s   |  j  j   s t d   |  j   } t | _ t | j  _ t | j  _ | j  j	 d t  t
 d | j  } | j |  | j   d |  _ d S(   s>   
        Deletes the records in the current QuerySet.
        s+   Cannot use 'limit' or 'offset' with delete.t   force_emptyR   N(   R   R   RM   RO   R?   R   R   t   select_for_updateRj   R   R   Rg   t   collectt   deleteR   R   (   R   t	   del_queryt	   collector(    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    		
c         C   s    t  j |  j  j |  |  d S(   s   
        Deletes objects found from the given queryset in single direct SQL
        query. No signals are sent, and there is no protection for cascades.
        N(   R   t   DeleteQueryR   t	   delete_qs(   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   _raw_delete  s    c         K   s   |  j  j   s t d   t |  _ |  j  j t j  } | j |  t	 j
 d |  j  sw t	 j d |  j  t } n t } zN | j |  j  j d  } | r t	 j d |  j  n t	 j d |  j  Wd | r t	 j d |  j  n  Xd |  _ | S(   s   
        Updates all elements in the current QuerySet, setting all the given
        fields to the appropriate values.
        s2   Cannot update a query once a slice has been taken.R   N(   R   R   RM   R?   R   R   R   t   UpdateQueryt   add_update_valuesR   R   Rg   R   R   Rx   t   execute_sqlR   R   R   R   R   (   R   R   R   R   t   rows(    (    s/   ../Django//lib/python/django/db/models/query.pyR   '  s$    				c         C   s_   |  j  j   s t d   |  j  j t j  } | j |  d |  _ | j	 |  j
  j d  S(   s  
        A version of update that accepts field objects instead of field names.
        Used primarily for model saving and not intended for use by general
        code (it requires too much poking around at model internals to be
        useful at that level).
        s2   Cannot update a query once a slice has been taken.N(   R   R   RM   R   R   R   t   add_update_fieldsR   R   Rx   Rg   R   (   R   t   valuesR   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   _updateC  s    		c         C   s2   |  j  d  k r% |  j j d |  j  St |  j   S(   NR   (   R   R   R   t   has_resultsRg   R=   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   existsR  s    c         C   s    t  |  j |  j  t |  _ d  S(   N(   t   prefetch_related_objectsR   R   R?   R   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR4   W  s    c         G   s   |  j  d t d t d |  S(   Nt   klasst   setupt   _fields(   RO   t   ValuesQuerySetR?   (   R   Rp   (    (    s/   ../Django//lib/python/django/db/models/query.pyR   `  s    c      
   O   s}   | j  d t  } | r4 t d t |  f   n  | r[ t |  d k r[ t d   n  |  j d t d t d | d |  S(   Nt   flats/   Unexpected keyword arguments to values_list: %si   sH   'flat' is not valid when values_list is called with more than one field.R   R   R   (   R   R   RJ   R-   R*   RO   t   ValuesListQuerySetR?   (   R   Rp   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   values_listc  s    t   ASCc         C   sX   | d k s t  d   | d k s0 t  d   |  j d t d	 t d
 | d | d |  S(   s   
        Returns a list of datetime objects representing all available dates for
        the given field_name, scoped to 'kind'.
        t   montht   yeart   days/   'kind' must be one of 'year', 'month' or 'day'.R   t   DESCs'   'order' must be either 'ASC' or 'DESC'.R   R   t   _field_namet   _kindt   _order(   s   months   years   day(   R   R   (   RM   RO   t   DateQuerySetR?   (   R   R   t   kindt   order(    (    s/   ../Django//lib/python/django/db/models/query.pyt   datesm  s    		c         C   s   |  j  d t  S(   s,   
        Returns an empty QuerySet.
        R   (   RO   RZ   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   noney  s    c         C   s
   |  j    S(   s   
        Returns a new QuerySet that is a copy of the current one. This allows a
        QuerySet to proxy for a model manager in some cases.
        (   RO   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   all  s    c         O   s   |  j  t | |  S(   sb   
        Returns a new QuerySet instance with the args ANDed to the existing
        set.
        (   t   _filter_or_excludeR   (   R   RT   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         O   s   |  j  t | |  S(   sd   
        Returns a new QuerySet instance with NOT (args) ANDed to the existing
        set.
        (   R  R?   (   R   RT   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   exclude  s    c         O   sv   | s | r* |  j  j   s* t d   n  |  j   } | rY | j  j t | |    n | j  j t | |    | S(   Ns2   Cannot filter a query once a slice has been taken.(   R   R   RM   RO   t   add_qR   (   R   t   negateRT   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR    s    c         C   sR   t  | t  s t | d  r> |  j   } | j j |  | S|  j d |  Sd S(   so  
        Returns a new QuerySet instance with filter_obj added to the filters.

        filter_obj can be a Q object (or anything with an add_to_query()
        method) or a dictionary of keyword lookup arguments.

        This exists to support framework features such as 'limit_choices_to',
        and usually it will be more natural to use other methods.
        t   add_to_queryN(   RG   R   t   hasattrRO   R   R  R  R   (   R   t
   filter_objR   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   complex_filter  s
    
c         K   s:   | j  d t  } |  j   } t | j _ | | j _ | S(   sj   
        Returns a new QuerySet instance that will select objects with a
        FOR UPDATE lock.
        t   nowait(   R   R   RO   R?   R   R   t   select_for_update_nowait(   R   R   R	  R&   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s
    c         O   s   d | k r t  j d t  n  | j d d  } | rS t d t |  f   n  |  j   } | r | rz t d   n  | j j |  n t	 | j _
 | r | | j _ n  | S(   s   
        Returns a new QuerySet instance that will select related objects.

        If fields are specified, they must be ForeignKey fields and only those
        related objects are included in the selection.
        t   depthsR   The "depth" keyword argument has been deprecated.
Use related field names instead.i    s2   Unexpected keyword arguments to select_related: %ss7   Cannot pass both "depth" and fields to select_related()(   t   warningst   warnt   PendingDeprecationWarningR   RJ   R-   RO   R   t   add_select_relatedR?   Rj   Rc   (   R   Rp   R   R  R&   (    (    s/   ../Django//lib/python/django/db/models/query.pyRj     s     	
c         G   s8   |  j    } | d k r$ g  | _ n | j j |  | S(   sh  
        Returns a new QuerySet instance that will prefetch the specified
        Many-To-One and Many-To-Many related objects when the QuerySet is
        evaluated.

        When prefetch_related() is called more than once, the list of lookups to
        prefetch is appended to. If prefetch_related(None) is called, the
        the list is cleared.
        N(   N(   RO   R   R   R3   (   R   t   lookupsR   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   prefetch_related  s
    
c         C   s   | j  j |  j  _ d S(   sp   
        Copies the related selection status from the QuerySet 'other' to the
        current QuerySet.
        N(   R   Rj   (   R   R^   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   dup_select_related  s    c   	      O   s  x@ | D]8 } | j  | k r2 t d | j    n  | | | j  <q Wt |  d d  } | d k r| t |  j j j    } n  x- | D]% } | | k r t d |   q q W|  j   } | j	 t
 |   x9 | j   D]+ \ } } | j j | |  j | d t q W| S(   s   
        Return a query set in which the returned objects have been annotated
        with data aggregated from related fields.
        sQ   The named annotation '%s' conflicts with the default name for another annotation.R   s8   The annotation '%s' conflicts with a field on the model.R   N(   R   R   R   R   Ru   R   Ro   t   get_all_field_namesRO   t   _setup_aggregate_queryR-   R"   R   R   R   (	   R   RT   R   R   t   namesR   R&   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   annotate  s$    c         G   sH   |  j  j   s t d   |  j   } | j  j   | j  j |   | S(   sL   
        Returns a new QuerySet instance with the ordering changed.
        s3   Cannot reorder a query once a slice has been taken.(   R   R   RM   RO   R   R   (   R   t   field_namesR&   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    	c         G   s;   |  j  j   s t d   |  j   } | j  j |   | S(   sY   
        Returns a new QuerySet instance that will select only distinct results.
        s:   Cannot create distinct fields once a slice has been taken.(   R   R   RM   RO   t   add_distinct_fields(   R   R  R&   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   distinct  s
    	c         C   sJ   |  j  j   s t d   |  j   } | j  j | | | | | |  | S(   s8   
        Adds extra SQL fragments to the query.
        s1   Cannot change a query once a slice has been taken(   R   R   RM   RO   t	   add_extra(   R   t   selectt   whereR   t   tablesR   t   select_paramsR   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   extra&  s
    	c         C   s#   |  j    } | j j | j _ | S(   s8   
        Reverses the ordering of the QuerySet.
        (   RO   R   t   standard_ordering(   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   reverse1  s    c         G   s<   |  j    } | d k r( | j j   n | j j |  | S(   sU  
        Defers the loading of data for certain fields until they are accessed.
        The set of fields to defer is added to any existing set of deferred
        fields. The only exception to this is if None is passed in as the only
        parameter, in which case all deferrals are removed (None acts as a
        reset option).
        N(   N(   RO   R   R   t   clear_deferred_loadingt   add_deferred_loading(   R   Rp   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   defer9  s
    c         G   s;   | d k r t d   n  |  j   } | j j |  | S(   s   
        Essentially, the opposite of defer. Only the fields passed into this
        method and that are not already specified as deferred are loaded
        immediately when the queryset is evaluated.
        s*   Cannot pass None as an argument to only().N(   N(   R   RJ   RO   R   t   add_immediate_loading(   R   Rp   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   onlyH  s
    c         C   s   |  j    } | | _ | S(   sY   
        Selects which database this QuerySet should excecute its query against.
        (   RO   R   (   R   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR   V  s    	c         C   sF   |  j  j s |  j  j r t S|  j  j r> |  j  j j j r> t St Sd S(   s   
        Returns True if the QuerySet is ordered -- i.e. has an order_by()
        clause or a default ordering on the model.
        N(	   R   t   extra_order_byR   R?   t   default_orderingR   Ro   t   orderingR   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   orderedb  s
    c         C   s;   |  j  r" |  j p! t j |  j  S|  j p: t j |  j  S(   sC   Return the database that will be used if this query is executed now(   R   R   R   t   db_for_writeR   t   db_for_read(   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyRg   o  s    	c         C   s   | s
 d St  |  j j } | p8 t | j | |  d  } x` g  t d t |  |  D] } | | | | !^ qW D]( } |  j j j	 | d | d |  j qr Wd S(   s   
        A little helper method for bulk_insert to insert the bulk one batch
        at a time. Inserts recursively a batch from the front of the bulk and
        then _batched_insert() the remaining objects again.
        Ni   i    Rp   R   (
   R   Rg   t   opst   maxt   bulk_batch_sizet   rangeR*   R   t   _base_managert   _insert(   R   R   Rp   R   R-  R   t   batch(    (    s/   ../Django//lib/python/django/db/models/query.pyR   y  s    !7c         K   s   | d  k r |  j } n  |  j j   } |  j r< t | _ n  | d |  j d | d |  j  } |  j	 | _	 |  j
 | _
 |  j | _ | j j |  | r t | d  r | j   n  | S(   NR   R   R   t   _setup_query(   R   R    R   R   R   R?   t   filter_is_stickyR   R   R   R   R   R!   R   R  R4  (   R   R   R   R   R   t   c(    (    s/   ../Django//lib/python/django/db/models/query.pyRO     s    	!c         C   sg   |  j  rc y: x3 t | p t  D] } |  j j t |  j    q WWqc t k
 r_ d |  _  qc Xn  d S(   sv   
        Fills the result cache with 'num' more entries (or until the results
        iterator is exhausted).
        N(   R   R0  t   ITER_CHUNK_SIZER   Rs   R>   R9   R   (   R   RC   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR:     s    	!c         C   s   t  |  _ |  S(   s  
        Indicates that the next filter call and the one following that should
        be treated as a single filter. This is only important when it comes to
        determining when to reuse tables for many-to-many filters. Required so
        that we can filter naturally on the results of related managers.

        This doesn't return a clone of the current QuerySet (it returns
        "self"). The method is only used internally and should be immediately
        followed by a filter() that does create a clone.
        (   R?   R   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   _next_is_sticky  s    	c         C   s   d S(   s   
        Checks that we are merging two comparable QuerySet classes. By default
        this does nothing, but see the ValuesQuerySet for an example of where
        it's useful.
        N(    (   R   R^   (    (    s/   ../Django//lib/python/django/db/models/query.pyRY     s    c         C   s@   x9 | j  j   D]( \ } } |  j  j | i   j |  q Wd S(   sX   
        Keep track of all known related objects from either QuerySet instance.
        N(   R   R"   t
   setdefaultR   (   R   R^   R   t   objects(    (    s/   ../Django//lib/python/django/db/models/query.pyR[     s    c         C   sd   |  j  j } |  j j d k r` g  | j D] } | j ^ q( } |  j j | t  |  j j	   n  d S(   s_   
        Prepare the query for computing a result that contains aggregate annotations.
        N(
   R   Ro   R   t   group_byR   Rp   Rw   t
   add_fieldsR   t   set_group_by(   R   t
   aggregatest   optsR   R  (    (    s/   ../Django//lib/python/django/db/models/query.pyR    s
    c         C   s   |  S(   N(    (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   _prepare  s    c         C   sZ   |  j  d  } | j d k s1 | t | j k rJ | j j d |  j   St d   d S(   sO   
        Returns the internal query's SQL and parameters (as a tuple).
        R   R   s2   Can't do subqueries with queries on different DBs.N(   R   R   R   R   R   Rx   t   as_nested_sqlR   (   R   R   R&   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   _as_sql  s    "N(E   t   __name__t
   __module__t   __doc__R   R   R)   R,   R1   R5   R8   R6   R@   RB   RF   RX   R`   Rb   R2   R   R   R   R   R   R   R   R   R   R?   t   alters_dataR   R   R   R   R4   R   R   R   R   R   R   R  R  R  R   Rj   R  R  R  R   R  R  R!  R$  R&  R   R*  t   propertyRg   R   R   RO   R:   R8  RY   R[   R  R@  RB  t   value_annotation(    (    (    s/   ../Django//lib/python/django/db/models/query.pyR       s   										.				c				
2	"														
	
												

					
					
		R   c           B   s_   e  Z d    Z d   Z d   Z d   Z d	 e d  Z d   Z	 d   Z
 d   Z d   Z RS(
   c         O   s)   t  t |   j | |   t |  j _ d  S(   N(   t   superR   R   R   R   Rj   (   R   RT   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         c   sy   t  |  j j  } |  j } t  |  j j  } | | | } x7 |  j j |  j  j   D] } t t	 | |   VqW Wd  S(   N(
   R-   R   Rl   R  Rm   Rx   Rg   Rz   Rk   R|   (   R   t   extra_namesR  t   aggregate_namesR  R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR2     s    	"c         C   s   t  d   d  S(   NsA   Queries with .values() or .values_list() applied can't be deleted(   RJ   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         C   s  |  j  j   |  j  j   |  j r g  |  _ g  |  _ |  j  j rd |  j  j rd t |  j  |  _	 q$t
 |  j  _ g  |  _	 x |  j D]` } | |  j  j k r |  j j |  q | |  j  j k r |  j j |  q |  j	 j |  q Wn: d |  _ g  |  j j j D] } | j ^ q|  _	 d |  _ g  |  j  _ |  j d k	 rU|  j  j |  j  n  |  j  j |  j	 t  |  j d k	 r|  j  j |  j  n  d S(   s   
        Constructs the field_names list that the values query will be
        retrieving.

        Called by the _clone() method after initializing the rest of the
        instance.
        N(   R   R"  t   clear_select_fieldsR   RJ  RK  R  R>  R-   R  R   t   default_colsRs   Rm   R   R   Ro   Rp   Rw   R  t   set_extra_maskR<  R?   t   set_aggregate_mask(   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR4    s0    					(	c         K   s   t  t |   j | |  } t | d  s: |  j | _ n  |  j | _ |  j | _ |  j | _ | r t | d  r | j   n  | S(   sH   
        Cloning a ValuesQuerySet preserves the current fields.
        R   R4  (	   RI  R   RO   R  R   R  RJ  RK  R4  (   R   R   R   R   R6  (    (    s/   ../Django//lib/python/django/db/models/query.pyRO   !  s    c         C   s   t  t |   j |  t |  j  t | j  k sd t |  j  t | j  k sd |  j | j k r} t d |  j j	   n  d  S(   Ns?   Merging '%s' classes must involve the same values in each case.(
   RI  R   RY   Ru   RJ  R  RK  RJ   R    RC  (   R   R^   (    (    s/   ../Django//lib/python/django/db/models/query.pyRY   1  s    c         C   s\   |  j  j   |  j d k	 rB |  j j |  |  j  j |  j  n  t t |   j |  d S(   s_   
        Prepare the query for computing a result that contains aggregate annotations.
        N(	   R   R=  RK  R   R3   RO  RI  R   R  (   R   R>  (    (    s/   ../Django//lib/python/django/db/models/query.pyR  9  s
    c         C   s   |  j  r t |  j   d k sC |  j  r\ t |  j j j  d k r\ t d |  j j   n  |  j   } | j	 d k s | t | j	 k r | j j d |  j   St d   d S(   s^  
        For ValueQuerySet (and subclasses like ValuesListQuerySet), they can
        only be used as nested queries if they're already set up to select only
        a single field (in which case, that is the field column that is
        returned). This differs from QuerySet.as_sql(), where the column to
        select is set up by Django.
        i   s.   Cannot use a multi-field %s as a filter value.R   s2   Can't do subqueries with queries on different DBs.N(   R   R*   R   Ro   Rp   RJ   R    RC  RO   R   R   R   R   Rx   RA  R   (   R   R   R&   (    (    s/   ../Django//lib/python/django/db/models/query.pyRB  E  s    %"c         C   s`   |  j  r t |  j   d k sC |  j  r\ t |  j j j  d k r\ t d |  j j   n  |  S(   s   
        Validates that we aren't trying to do a query like
        value__in=qs.values('value1', 'value2'), which isn't valid.
        i   s.   Cannot use a multi-field %s as a filter value.(   R   R*   R   Ro   Rp   RJ   R    RC  (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR@  W  s
    %N(   RC  RD  R   R2   R   R4  R   R   RO   RY   R  RB  R@  (    (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s   				,			R   c           B   s   e  Z d    Z d   Z RS(   c   	      c   s  |  j  rP t |  j  d k rP x\|  j j |  j  j   D] } | d Vq: Wn-|  j j r |  j j r x|  j j |  j  j   D] } t	 |  Vq Wn t
 |  j j  } |  j } t
 |  j j  } | | | } |  j rt
 |  j  g  | D] } | |  j k r | ^ q } n | } xZ |  j j |  j  j   D]= } t t | |   } t	 g  | D] } | | ^ qa Vq<Wd  S(   Ni   i    (   R   R*   R   R   Rx   Rg   Rz   Rl   Rm   t   tupleR-   R  Rk   R|   (	   R   R   RJ  R  RK  R  R   Rp   R0   (    (    s/   ../Django//lib/python/django/db/models/query.pyR2   d  s     ""		8"c         O   s=   t  t |   j | |   } t | d  s9 |  j | _ n  | S(   NR   (   RI  R   RO   R  R   (   R   RT   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyRO     s    (   RC  RD  R2   RO   (    (    (    s/   ../Django//lib/python/django/db/models/query.pyR   c  s   	R   c           B   s)   e  Z d    Z d   Z d e d  Z RS(   c         C   s   |  j  j |  j  j   S(   N(   R   Rx   Rg   Rz   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR2     s    c         C   s]   |  j  j   |  j  j d t j d t  |  _  g  |  j  _ |  j  j |  j |  j	 |  j
  d S(   s   
        Sets up any special features of the query attribute.

        Called by the _clone() method after initializing the rest of the
        instance.
        R   R   N(   R   R"  R   R   t	   DateQueryR?   R  t   add_date_selectR   R   R   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR4    s    !c         K   s\   t  t |   j | t |  } |  j | _ |  j | _ | rX t | d  rX | j   n  | S(   NR4  (   RI  R   RO   R   R   R   R  R4  (   R   R   R   R   R6  (    (    s/   ../Django//lib/python/django/db/models/query.pyRO     s    N(   RC  RD  R2   R4  R   R   RO   (    (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s   		RZ   c           B   s   e  Z d d d d   Z d   Z d   Z d   Z 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 d d d d d d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e Z RS(   c         C   s)   t  t |   j | | |  g  |  _ d  S(   N(   RI  RZ   R   R   (   R   R   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         C   s
   |  j    S(   N(   RO   (   R   R^   (    (    s/   ../Django//lib/python/django/db/models/query.pyR`     s    c         C   s
   | j    S(   N(   RO   (   R   R^   (    (    s/   ../Django//lib/python/django/db/models/query.pyRb     s    c         C   s   d S(   Ni    (    (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         C   s   d  S(   N(    (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         K   s.   t  t |   j | d | | } g  | _ | S(   NR   (   RI  RZ   RO   R   (   R   R   R   R   R6  (    (    s/   ../Django//lib/python/django/db/models/query.pyRO     s    !	c         c   s   t  t g    Vd  S(   N(   R>   R7   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR2     s    c         C   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         O   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   RT   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         O   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   RT   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR    s    c         C   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   R  (    (    s/   ../Django//lib/python/django/db/models/query.pyR    s    c         O   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   Rp   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyRj     s    c         O   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   RT   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR    s    c         G   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   R  (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         G   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   R  (    (    s/   ../Django//lib/python/django/db/models/query.pyR    s    c         C   s   |  j  j   s t d   |  S(   s/   
        Always returns EmptyQuerySet.
        s1   Cannot change a query once a slice has been taken(   R   R   RM   (   R   R  R  R   R  R   R  (    (    s/   ../Django//lib/python/django/db/models/query.pyR    s    	c         C   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR!    s    c         G   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   Rp   (    (    s/   ../Django//lib/python/django/db/models/query.pyR$    s    c         G   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   Rp   (    (    s/   ../Django//lib/python/django/db/models/query.pyR&    s    c         K   s   d S(   s(   
        Don't update anything.
        i    (    (   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         O   sA   x | D] } | | | j  <q Wt g  | D] } | d f ^ q(  S(   sC   
        Return a dict mapping the aggregate names to None
        N(   R   Rk   R   (   R   RT   R   R   t   key(    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         G   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   Rp   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    c         O   s   |  S(   s/   
        Always returns EmptyQuerySet.
        (    (   R   Rp   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    N(   RC  RD  R   R   R`   Rb   R   R   R   RO   R2   R   R   R  R  Rj   R  R   R  R  R!  R$  R&  R   R   R   R   RH  (    (    (    s/   ../Django//lib/python/django/db/models/query.pyRZ     s2   																				i    c         C   sS  | r" | d k r" | | k r" d S| r | j |   p= t   } xE |  j j   D]+ } | j |  } | rP | j |  qP qP Wn d } | r:t   }	 g  }
 xg |  j j   D]V \ } } | j | k r |	 j | j	  q | r | d k	 r q q |
 j
 | j	  q Wt |
  } |	 r1t |  |	  }  |
 } qd	 } ny | rt |  j j  t |  j j  k rt |  j j  } g  |  j j D] } | j	 ^ q} n t |  j j  } d
 } | d k	 } g  } x |  j j D]{ } t | | | |  r| r| | j } n d } t | j j d | d | d d | d | } | j
 | | f  qqWg  } | rx |  j j   D] } | j j rmt | j | | | j | j  d t rm| | j j   } t | j d | d | d d | d | d t } | j
 | j | f  qmqmWn  | r.| j |  j j j	  } n |  j j   } |  | | | | | f S(   s  
    Helper function that recursively returns an information for a klass, to be
    used in get_cached_row.  It exists just to compute this information only
    once for entire queryset. Otherwise it would be computed for each row, which
    leads to poor perfomance on large querysets.

    Arguments:
     * klass - the class to retrieve (and instantiate)
     * max_depth - the maximum depth to which a select_related()
       relationship should be explored.
     * cur_depth - the current depth in the select_related() tree.
       Used in recursive calls to determin if we should dig deeper.
     * requested - A dictionary describing the select_related() tree
       that is to be retrieved. keys are field names; values are
       dictionaries describing the keys on that related object that
       are themselves to be select_related().
     * only_load - if the query has had only() or defer() applied,
       this is the list of field names that will be returned. If None,
       the full field list for `klass` can be assumed.
     * local_only - Only populate local fields. This is used when
       following reverse select-related relations
    Rc   t	   cur_depthi   Rd   Re   R!  t
   local_onlyN(    (    (   R   R   Ru   Ro   t   get_parent_listR   Rq   Rr   Rv   Rw   Rs   R*   R	   R   Rp   R   Ry   t   relt   tot   get_all_related_objectsR   t   uniqueR   R?   t   related_query_namet   indexR   t   pk_index(   R   Rc   RT  Rd   Re   RU  R   t   parentRp   R   R   R   R   t   field_countR  R   t
   restrictedt   related_fieldsR>   R   t   reverse_related_fieldsR   t   pk_idx(    (    s/   ../Django//lib/python/django/db/models/query.pyRy   %  sf    				*% c      	   C   s  | d k r d S| \ } } } } }	 }
 |  | | | !} | |
 d k sY | |
 d k rb d } n0 | r | t t | |     } n | |   } | r | | j _ t | j _ n  | | | } x | D] \ } } t |  | | |  } | r | \ } } | d k	 r t | | j	   |  n  | j
 rT| d k	 rTt | | j j	   |  qTq q WxL|	 D]D\ } } t |  | | |  } | r_| \ } } | d k	 rt | | j j	   |  n  | d k	 rt | | j	   |  | j } t | d  r| j j } n  x | j   D] \ } } | d k	 rt | | j t | | j   | j ry/ t | | j	    } t | | j	   |  Wqt k
 rqXqqqWqq_q_W| | f S(   s  
    Helper function that recursively returns an object with the specified
    related attributes already populated.

    This method may be called recursively to populate deep select_related()
    clauses.

    Arguments:
         * row - the row of data returned by the database cursor
         * index_start - the index of the row at which data for this
           object is known to start
         * offset - the number of additional fields that are known to
           exist in row for `klass`. This usually means the number of
           annotated results on `klass`.
        * using - the database alias on which the query is being executed.
         * klass_info - result of the get_klass_info function
    t    t	   _deferredN(   R   Rk   R|   R}   Rg   R   R~   R{   R   t   get_cache_nameRZ  t   relatedRo   R   t   proxy_for_modelRq   Rw   RW  t   AttributeError(   R   R   R   R   Rf   R   R  R_  Ra  Rb  Rc  Rp   R&   t	   index_endR   t
   cached_rowR   R?  t	   rel_fieldt	   rel_modelt
   cached_obj(    (    s/   ../Django//lib/python/django/db/models/query.pyR{     sT     	#		t   RawQuerySetc           B   sw   e  Z d  Z d	 d	 d	 d	 d	 d  Z d   Z d   Z d   Z e d    Z	 d   Z
 e d    Z e d    Z RS(
   sp   
    Provides an iterator which converts the results of raw SQL queries into
    annotated model instances.
    c         C   sg   | |  _  | |  _ | |  _ | p? t j d | d |  j d |  |  _ | pN d |  _ | p] i  |  _ d  S(   NR   R   R   (    (	   t	   raw_queryR   R   R   t   RawQueryRg   R   R   t   translations(   R   Rp  R   R   R   Rr  R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR     s    			*c         c   s  i  } g  } |  j  } t | j j d  |  j t | |  } t | d  } t |  j  } xV t |  j  D]E \ } } | |  j	 k r | | |  j	 | j
 <ql | j | | f  ql Wt   }	 x9 |  j j j D]( }
 |
 j
 | k r |	 j |
 j
  q q W|	 r<|  j j j j
 |	 k r't d   n  t |  j |	  } n= |  j } g  } x+ |  j j j D] }
 | j | |
 j
  q[W| rg  |  j D] } |  j	 j | d   ^ q} n  x | D] } | r| j | |  } n  |	 ri  } x+ t j |  D] \ } } | | | | <qW| |   } n) g  | D] } | | ^ q%} | |   } | r{x+ | D]  \ } } t | | | |  qTWn  | | j _  t | j _ | VqWd  S(   Nt   SQLCompilert   resolve_columnss&   Raw query must include the primary key(   Rg   R   R-  R   R   R  R7   R   t   columnst   model_fieldsRw   Rs   Ru   R   Ro   Rp   Rv   R   R
   R	   R   R   Rt  R   t	   iteritemsR   R}   R   R~   (   R   t   model_init_field_namest   annotation_fieldsRg   R   t   need_resolv_columnsR   R;   t   columnR   R   R   t   model_init_field_posR6  Rp   R   t   model_init_kwargsRw   t   instancet   model_init_args(    (    s/   ../Django//lib/python/django/db/models/query.pyR8     sR    			.c         C   s   d |  j  t |  j  S(   Ns   <RawQuerySet: %r>(   Rp  RP  R   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR1   ?  s    c         C   s   t  |   | S(   N(   R-   (   R   R'   (    (    s/   ../Django//lib/python/django/db/models/query.pyRX   B  s    c         C   s   |  j  p t j |  j  S(   sC   Return the database that will be used if this query is executed now(   R   R   R,  R   (   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyRg   E  s    c         C   sC   t  |  j d |  j d |  j j d |  d |  j d |  j d | S(   s^   
        Selects which database this Raw QuerySet should excecute it's query against.
        R   R   R   R   Rr  (   Ro  Rp  R   R   R   R   Rr  (   R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyR   J  s    c         C   s   t  |  d  s{ |  j j   |  _ xW |  j j   D]C \ } } y# |  j j |  } | |  j | <Wq1 t k
 rs q1 Xq1 Wn  |  j S(   sg   
        A list of model field names in the order they'll appear in the
        query results.
        t   _columns(   R  R   t   get_columnsR  Rr  R"   R\  R   (   R   t
   query_namet
   model_nameR\  (    (    s/   ../Django//lib/python/django/db/models/query.pyRu  S  s    c         C   st   t  |  d  sm t |  j j j } i  |  _ x? |  j j j D]+ } | j	   \ } } | |  j | |  <q; Wn  |  j S(   sC   
        A dict mapping column names to model field names.
        t   _model_fields(
   R  R   Rg   t   introspectiont   table_name_converterR  R   Ro   Rp   t   get_attname_column(   R   t	   converterR   Rr   R{  (    (    s/   ../Django//lib/python/django/db/models/query.pyRv  g  s    	N(   RC  RD  RE  R   R   R8   R1   RX   RG  Rg   R   Ru  Rv  (    (    (    s/   ../Django//lib/python/django/db/models/query.pyRo    s   		C				c         C   s>   t  j |   } | j | | d | | j d |  j |  S(   s   
    Inserts a new record for the given model. This provides an interface to
    the InsertQuery class and is how Model.save() is implemented. It is not
    part of the public API.
    t   rawR   (   R   t   InsertQueryt   insert_valuesRx   R   (   R   R   Rp   t	   return_idR  R   R   (    (    s/   ../Django//lib/python/django/db/models/query.pyt   insert_queryu  s    c      
   C   s  t  |   d k r d S|  d j } t   } i  } g  } t   } t j | |  } x| D]z} | | k rr qZ n  | j |  |  }	 | j t  }
 x=t |
  D]/\ } } t  |	  d k r Pn  t	 } xI |	 D]A } t
 | d  sy i  | _ Wqt k
 rt } PqXq Pq W| sPn  |	 d } t | |  \ } } } } | slt d | | j j | f   n  | t  |
  d k r| d k rt d |   n  | d k	 rg| rgt j |
 d | d ! } | | k r| | }	 qt |	 | |  \ }	 } | | k o| | k sWx0 | D]( } t j | | g  } | j |  qW|	 | | <n  | j |  q g  } xZ |	 D]R } y t | |  } Wn t j k
 rqtn X| d k rqtn  | j |  qtW| }	 q WqZ Wd S(   s   
    Helper function for prefetch_related functionality

    Populates prefetched objects caches for a list of results
    from a QuerySet
    i    Nt   _prefetched_objects_cachesQ   Cannot find '%s' on %s object, '%s' is an invalid parameter to prefetch_related()i   so   '%s' does not resolve to a item that supports prefetching - this is an invalid parameter to prefetch_related().(   R*   R    Ru   t	   itertoolst   chainRv   t   splitR   R   R?   R  R  Ri  R   t   get_prefetcherRC  R   R   t   joint   prefetch_one_levelRs   R   R    t   ObjectDoesNotExist(   t   result_cachet   related_lookupsR   t   done_lookupst   done_queriest   auto_lookupst   followed_descriptorst   all_lookupsR   t   obj_listt   attrst   levelt   attrt   good_objectsR&   t	   first_objt
   prefetchert
   descriptort
   attr_foundt
   is_fetchedt   current_lookupt   additional_prlR   t   new_prlt   new_obj_listt   new_obj(    (    s/   ../Django//lib/python/django/db/models/query.pyR     st    		
"	c         C   s   d } t } t } t |  j | d  } | d k rc y t |  |  } t } Wq t k
 r_ q Xnf t } | r t | d  r | } | j |   r t } q q t |  |  } t | d  r | } q n  | | | | f S(   s  
    For the attribute 'attr' on the given instance, finds
    an object that has a get_prefetch_query_set().
    Returns a 4 tuple containing:
    (the object with get_prefetch_query_set (or None),
     the descriptor object representing this relationship (or None),
     a boolean that is False if the attribute was not found at all,
     a boolean that is True if the attribute has already been fetched)
    t   get_prefetch_query_setN(   R   R   R   R    R?   Ri  R  t	   is_cached(   R~  R  R  R  R  t   rel_obj_descriptorR   (    (    s/   ../Django//lib/python/django/db/models/query.pyR    s(    

c         C   s   | j  |   \ } } } } } t | d g   } | rB g  | _ n  t |  }	 i  }
 x3 |	 D]+ } | |  } |
 j | g   j |  q[ Wx |  D] } | |  } |
 j | g   } | r t | | | r | d n d  q t | |  j	   } | | _
 t | _ | | j | <q W|	 | f S(   sU  
    Helper function for prefetch_related_objects

    Runs prefetches on all instances using the prefetcher object,
    assigning results to relevant caches in instance.

    The prefetched objects are returned, along with any additional
    prefetches that must be done due to prefetch_related lookups
    found from default managers.
    R   i    N(   R  R   R   R-   R9  Rs   R   R   R   R   R   R?   R   R  (   t	   instancesR  Rw   t   rel_qst   rel_obj_attrt   instance_attrt   singlet
   cache_nameR  t   all_related_objectst   rel_obj_cacheR   t   rel_attr_valR&   t   instance_attr_valt   valsRV   (    (    s/   ../Django//lib/python/django/db/models/query.pyR    s&    #		(0   RE  R#   R  R   R  t   django.coreR    t	   django.dbR   R   R   R   t   django.db.models.constantsR   t   django.db.models.fieldsR   t   django.db.models.query_utilsR   R   R	   R
   t   django.db.models.deletionR   t   django.db.modelsR   t   django.utils.functionalR   t   django.utilsR   t
   CHUNK_SIZER7  R.   t   EmptyResultSett   objectR   R   R   R   RZ   R   R   Ry   R{   Ro  R  R   R  R  (    (    (    s/   ../Django//lib/python/django/db/models/query.pyt   <module>   sB   ""	   %	ga	r	+