o 3a0@sdZddlZddlZddlZddlZddlmZddlmZm Z ddl m Z ddl m Z ddlmZedd ZGd d d eZGd d d eedZddZGddde jZGdddZGdddZd ddZddZddZGdddZdS)!z Various data structures used in query construction. Factored out from django.db.models.query to avoid making the main module very large and/or so that they can be used by other modules without getting into circular import difficulties. N) namedtupleFieldDoesNotExist FieldError) LOOKUP_SEP)tree)RemovedInDjango40WarningPathInfozGfrom_opts to_opts target_fields join_field m2m direct filtered_relationcs<eZdZeddZddZfddZfddZZS) InvalidQueryTypecCsttfSNrselfr>/usr/lib/python3/dist-packages/django/db/models/query_utils.py _subclassesszInvalidQueryType._subclassescCstjdtdddS)Nz\The InvalidQuery exception class is deprecated. Use FieldDoesNotExist or FieldError instead.)category stacklevel)warningswarnrr rrr__warns  zInvalidQueryType.__warnc |t||jpt|Sr )_InvalidQueryType__warn isinstancersuper__instancecheck__)r instance __class__rrr&z"InvalidQueryType.__instancecheck__crr )r issubclassrr__subclasscheck__)r subclassrrrr!*rz"InvalidQueryType.__subclasscheck__) __name__ __module__ __qualname__propertyrrrr! __classcell__rrrrr s   r c@s eZdZdS) InvalidQueryN)r#r$r%rrrrr(/sr() metaclassccs(|V|D] }t|EdHqdSr )__subclasses__ subclassesclsr"rrrr+3s  r+csjeZdZdZdZdZeZdZdddfdd Zd d Z d d Z ddZ ddZ dddZ ddZZS)Qze Encapsulate filters as objects that can then be combined logically (using `&` and `|`). ANDORTNF) _connector_negatedcs&tjg|t|||ddS)N)children connectornegated)r__init__sorteditems)r r1r2argskwargsrrrr6Ds&z Q.__init__cCst|tst|dddust||s!t|dr|St|St|tr8|s8|\}}}t||i|St|}||_| ||| |||S)N conditionalFTcopy) rr.getattr TypeErrorhasattrr< deconstructtyper4add)r otherconn_r9r:objrrr_combineGs   z Q._combinecC|||jSr )rGr0r rCrrr__or__WzQ.__or__cCrHr )rGr/rIrrr__and__ZrKz Q.__and__cCs$t|}|||j||Sr )rArBr/negate)r rFrrr __invert__]s z Q.__invert__cCs&|j|||ddd\}}|||S)NF) allow_joins split_subqcheck_filterable)_add_q promote_joins)r queryrOreuse summarizefor_saveclausejoinsrrrresolve_expressioncs  zQ.resolve_expressioncCsfd|jj|jjf}|dr|dd}t|j}i}|j|jkr'|j|d<|j r.d|d<|||fS)Nz%s.%szdjango.db.models.query_utilszdjango.db.modelsr1Tr2) rr$r# startswithreplacetupler3r4defaultr5)r pathr9r:rrrr@ms      z Q.deconstruct)NTNFF)r#r$r%__doc__r/r0r^r;r6rGrJrLrNrZr@r'rrrrr.9s  r.c@s*eZdZdZddZd ddZddZdS) DeferredAttributez A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed. cCs ||_dSr )field)r rbrrrr6s zDeferredAttribute.__init__NcCsZ|dur|S|j}|jj}||vr)||}|dur%|j|gd||S|||<||S)zx Retrieve and caches the value from the datastore on the first lookup. Return the cached value. N)fields)__dict__rbattname_check_parent_chainrefresh_from_db)r rr-data field_namevalrrr__get__s zDeferredAttribute.__get__cCs6|j}||jj}|jjr|j|krt||jSdS)z Check if the field value can be fetched from a parent field already loaded in the instance. This can be done if the to-be fetched field is a primary key field. N)_metaget_ancestor_linkrbmodel primary_keyr=re)r ropts link_fieldrrrrfs  z%DeferredAttribute._check_parent_chainr )r#r$r%r`r6rkrfrrrrrazs   rac@steZdZeddZeejddddZddZd d Z e d d Z ed dZ edddZ edddZdS)RegisterLookupMixincCs||dSr ) get_lookupsget)r- lookup_namerrr _get_lookupszRegisterLookupMixin._get_lookupN)maxsizecCsddt|D}||S)NcSsg|] }|jdiqS) class_lookups)rdrt).0parentrrr sz3RegisterLookupMixin.get_lookups..)inspectgetmro merge_dicts)r-rxrrrrss zRegisterLookupMixin.get_lookupscCNddlm}||}|durt|dr|j|S|dur%t||s%dS|S)Nr)Lookup output_field)django.db.models.lookupsrrvr?r get_lookupr )r rurfoundrrrr   zRegisterLookupMixin.get_lookupcCr)Nr) Transformr)rrrvr?r get_transformr )r rurrrrrrrz!RegisterLookupMixin.get_transformcCs i}t|D]}||q|S)z Merge dicts in reverse to preference the order of the original list. e.g., merge_dicts([a, b]) will preference the keys in 'a' over those in 'b'. )reversedupdate)dictsmergeddrrrr~s  zRegisterLookupMixin.merge_dictscCst|D]}|jqdSr )r+rs cache_clearr,rrr_clear_cached_lookupss  z)RegisterLookupMixin._clear_cached_lookupscCs4|dur|j}d|jvri|_||j|<||S)Nrx)rurdrxrr-lookuprurrrregister_lookups  z#RegisterLookupMixin.register_lookupcCs|dur|j}|j|=dS)zn Remove given lookup from cls lookups. For use in tests only as it's not thread-safe. N)rurxrrrr_unregister_lookups z&RegisterLookupMixin._unregister_lookupr )r#r$r% classmethodrv functools lru_cachersrr staticmethodr~rrrrrrrrrs         rrFcCs|jsdS|jjr |s dS|r"|r||vrdS|s"|j|vr"dS|s)|jr)dS|rE|j|vrE|rE|j|vrEd|jjj|jf}t |dS)au Return True if this field should be used to descend deeper for select_related() purposes. Used by both the query construction code (sql.query.fill_related_selections()) and the model instance creation code (query.get_klass_info()). Arguments: * field - the field to be checked * restricted - a boolean field, indicating if the field list has been manually restricted using a requested clause) * requested - The select_related() dictionary. * load_fields - the set of fields to be loaded on this model * reverse - boolean, True if we are checking a reverse select related FzXField %s.%s cannot be both deferred and traversed using select_related at the same time.T) remote_field parent_linkrelated_query_namenamenullrernrl object_namer)rb restricted requested load_fieldsreversemsgrrrselect_related_descends&   rcCsVtdt|dD]}t|d|}||vr(||r(||||dfSq dS)z Check if the lookup_parts contains references to the given annotations set. Because the LOOKUP_SEP is contained in the default annotation names, check each prefix of the lookup_parts for a match. rN)Fr)rangelenrjoin) lookup_parts annotationsnlevel_n_lookuprrrrefs_expression s rcs,fdd}||pt|ddo||jjS)z Check that self.model is compatible with target_opts. Compatibility is OK if: 1) model and opts match (where proxy inheritance is removed) 2) model is parent of opts' model or the other way around cs*jj|jkp|jjvp|vSr )rlconcrete_modelget_parent_list)rprnrrchecks  z-check_rel_lookup_compatibility..checkroF)r=rnrl)rn target_optsrbrrrrcheck_rel_lookup_compatibilitys rc@s@eZdZdZedddZddZddZd d Zd d Z d S)FilteredRelationz7Specify custom filtering in the ON clause of SQL joins. conditioncCs:|std||_d|_t|tstd||_g|_dS)Nzrelation_name cannot be empty.z*condition argument must be a Q() instance.) ValueError relation_namealiasrr.rr_)r rrrrrr66s  zFilteredRelation.__init__cCs4t||jstS|j|jko|j|jko|j|jkSr )rrNotImplementedrrrrIrrr__eq__@s    zFilteredRelation.__eq__cCs,t|j|jd}|j|_|jdd|_|S)Nr)rrrrr_)r clonerrrrIszFilteredRelation.clonecOstd)zz QuerySet.annotate() only accepts expression-like arguments (with a resolve_expression() method). z0FilteredRelation.resolve_expression() is unused.)NotImplementedError)r r9r:rrrrZOsz#FilteredRelation.resolve_expressioncCs&|j}|j|jt|jd}||S)N)rU)rTbuild_filtered_relation_qrsetr_compile)r compiler connectionrTwhererrras_sqlVs zFilteredRelation.as_sqlN) r#r$r%r`r.r6rrrZrrrrrr3s   r)F)r`r<rr|r collectionsrdjango.core.exceptionsrrdjango.db.models.constantsr django.utilsrdjango.utils.deprecationrr rAr Exceptionr(r+Noder.rarrrrrrrrrrs*     A( C%