o 3a@sddlZddlZddlZddlmZddlmZddlmZddl m Z m Z ddl m Z ddlmZmZmZmZddlmZdd lmZmZdd lmZdd lmZdd lmZmZdd lm Z e!Z"dZ#dZ$dZ%ddZ&ddZ'GdddZ(dS)N) defaultdict)apps)settings)FieldDoesNotExistImproperlyConfigured) connections) AutoFieldManagerOrderWrtUniqueConstraint)PathInfo) ImmutableList OrderedSet)cached_property) import_string)camel_case_to_spaces format_lazy)overridezyThe return type of '%s' should never be mutated. If you want to manipulate this list for your own use, make a copy first.) verbose_nameverbose_name_pluraldb_tableorderingunique_together permissions get_latest_byorder_with_respect_to app_label db_tablespaceabstractmanagedproxy swappable auto_createdindex_togetherrdefault_permissionsselect_on_savedefault_related_namerequired_db_featuresrequired_db_vendorbase_manager_namedefault_manager_nameindexes constraintscCsdz&|sWdSt|ttfst|d}t|ttfs|f}tdd|DWSty1|YSw)z option_together can be either a tuple of tuples, or a single tuple of two strings. Normalize it to a tuple of tuples, so that calling code can uniformly expect that. rrcss|]}t|VqdSN)tuple).0otrr:/usr/lib/python3/dist-packages/django/db/models/options.py 5z%normalize_together..) isinstancer/list TypeError)option_together first_elementrrr2normalize_together&s r:cCst|t|dS)N)warning)r IMMUTABLE_WARNING)namedatarrr2make_immutable_fields_list<sr?c@seZdZhdZhdZeZd\ddZeddZ edd Z ed d Z ed d Z ddZ ddZddZddZddZd]ddZddZddZdd Zd!d"Zd#d$Zed%d&Zed'd(Zed)d*Zed+d,Zed-d.Zed/d0Zed1d2Zed3d4Z ed5d6Z!ed7d8Z"ed9d:Z#ed;d<Z$ed=d>Z%d?d@Z&dAdBZ'dCdDZ(dEdFZ)dGdHZ*dIdJZ+dKdLZ,edMdNZ-d^dPdQZ.d_dRdSZ/  d`dTdUZ0edVdWZ1edXdYZ2edZd[Z3dS)aOptions> fieldsmanagers base_manager managers_map many_to_manyconcrete_fieldsdefault_manager_forward_fields_maplocal_concrete_fields> fields_map_relation_treerelated_objectsNcCsi|_g|_g|_g|_g|_d|_d|_d|_d|_d|_ d|_ g|_ d|_ g|_ g|_g|_g|_d|_d|_g|_d|_||_d|_d|_tj|_g|_d|_||_d|_d|_d|_ d|_!d|_"d|_#d|_$d|_%i|_&d|_'g|_(|j)|_*d|_+dS)NF)addchangedeleteviewT),_get_fields_cache local_fieldslocal_many_to_manyprivate_fieldslocal_managersr*r+ model_namerrrr_ordering_clashr,r-rr$r&r%r object_namerrrrDEFAULT_TABLESPACErr(r)metapk auto_fieldrr r!proxy_for_modelconcrete_modelr"parentsr#related_fkey_lookups default_appsrr')selfr[rrrr2__init__JsR zOptions.__init__cCd|j|jfSN%s.%s)rrYrcrrr2labelz Options.labelcCrerf)rrWrhrrr2 label_lowerrjzOptions.label_lowercCs|jj|jSr.)r app_configsgetrrhrrr2 app_configszOptions.app_configcCs |jduSr.)rnrhrrr2 installeds zOptions.installedcCsddlm}ddlm}||_||_|j|_|j|_ t |j|_ i|_ |j r|j j}|j jD] }|dr<||=q2tD]0}||vrWt||||t|||j |<q?t|j |rot||t|j |t|||j |<q?t|j|_t|j|_t|jddsdD]}t||g}t|||||q|jdurtd|j |_t|jo|j|_|ikrt d d !|ntd|j |_|` |j"sd |j#|j f|_"||j"|j$%|_"dSdS) Nr connection) truncate_name_rF>r,r-z{}sz)'class Meta' got invalid attribute(s): %s,z%s_%s)& django.dbrqdjango.db.backends.utilsrr_metamodel__name__rYlowerrWrroriginal_attrsr[__dict__copy startswith DEFAULT_NAMESsetattrpopgetattrhasattrr:rr$_format_names_with_classrrboolrrrXr7joinrropsmax_name_length)rcclsr=rqrr meta_attrs attr_nameobjsrrr2contribute_to_classsP            zOptions.contribute_to_classcCsDg}|D]}|}|j|jj|jd|_||q|S)z4App label/class name interpolation for object names.)rclass)cloner=rwrrzryappend)rcrrnew_objsobjrrr2rs  z Options._format_names_with_classc Cst|jdtj}|jr|jjrt|j}|jd|jd}nd}|s*t|dzt |}Wnt yI}z |d|d}t||d}~wwt |t sZt d|d |d |S) Ndefault_auto_field.z.default_auto_fieldDEFAULT_AUTO_FIELDz must not be empty.z refers to the module 'z' that could not be imported.z Primary key 'z' referred by z must subclass AutoField.)rrnrr!_is_default_auto_field_overriddentype __module__ __qualname__rr ImportError issubclassr ValueError)rc pk_class_pathapp_config_classsourcepk_classemsgrrr2_get_default_pk_classs6    zOptions._get_default_pk_classcs|jr?|jztfdd|jddD|_Wnty(td|jfwd|_tdd|jj Ds>| dt nd|_|j dur|j rmtt|j fd d |j D}|rc|d d _|dS|}|d d d d}| d|dSdS)Nc3s(|]}|jks|jkr|VqdSr.)r=attnamer0f)queryrr2r3sz#Options._prepare..Freverse%s has no field named '%s')_ordercss|]}t|tVqdSr.)r5r r0fieldrrr2r3 rcsg|] }|jjkr|qSrr=)r0fld)rrr2 sz$Options._prepare..rTID)r primary_keyr#id)rnext _get_fields StopIterationrrYranyrwrS add_to_classr r\r`itervaluesrsetup_pkr)rcrxalready_createdrautor)rrr2_prepares4    zOptions._preparecCs|j||dSr.)rVr _expire_cache)rcmanagerrrr2 add_manager!s  zOptions.add_managerFcCs|r |j|n|jr|jrt|j|n t|j||||jrKt |j drK|j j rKz |j j j j ddWn tyDYnw| dS|j dddS)NrxF)forwardr)rUr is_relationrEbisectinsortrTrSrr remote_fieldrxrwrAttributeError)rcrprivaterrr2 add_field%s     zOptions.add_fieldcCs$|js|jr||_d|_dSdSdS)NF)r\r serialize)rcrrrr2rCs  zOptions.setup_pkcCs|jj|_||_|jj|_dS)zb Do the internal setup so that the current model is a proxy for "target". N)rwr\r^r)rctargetrrr2 setup_proxyHs zOptions.setup_proxycCs d|jS)Nz)rYrhrrr2__repr__Qs zOptions.__repr__cCs|jSr.)rkrhrrr2__str__TszOptions.__str__cs\|js |js |js dSttrt|jr|jjkS|jr,t fdd|jDSdS)z Return True if the model can/should be migrated on the `connection`. `connection` can be either a real connection or a connection alias. Fc3s|] }tj|dVqdS)FN)rfeatures)r0featrprr2r3csz&Options.can_migrate..T) r!swappedr r5strrr)vendorr(all)rcrqrrpr2 can_migrateWs   zOptions.can_migratecCs6td t|jWdS1swYdS)z%Return the untranslated verbose name.N)rrrrhrrr2verbose_name_rawgs $zOptions.verbose_name_rawcCs`|jr.tt|jd}|r.z |d\}}Wn ty |YSwd||f|jkr.|SdS)a! Has this model been swapped out for another? If so, return the model name of the replacement; otherwise, return None. For historical reasons, model name lookups using get_model() are case insensitive, so we make sure we are case insensitive here. Nrrg)r"rrsplitrrzrk)rc swapped_for swapped_labelswapped_objectrrr2rms  zOptions.swappedcCsg}t}dd|jD}t|D])\}}|jjD] }|j|vr#qt|}|j|_||j| ||j |fqqt dddt |DS)Ncss|] }t|dr|VqdS)rwN)r)r0brrr2r3sz#Options.managers..rBcss|]}|dVqdS)Nr)r0mrrr2r3r4) setrxmro enumeraterwrVr=r}rNrcreation_counterr?sorted)rcrB seen_managersbasesdepthbaserrrr2rBs      zOptions.managerscCdd|jDS)NcSsi|]}|j|qSrr)r0rrrr2 sz(Options.managers_map..)rBrhrrr2rDrjzOptions.managers_mapcCs|j}|s"|jddD]}t|dr!|jjdkr|jj}nq|r;z|j|WSty:td|j |fwt }d|_|j|_d|_ |S)Nrw _base_manager%s has no manager named %rT) r*rxrrrr=rDKeyErrorrrYr r#)rcr*parentrrrr2rCs0    zOptions.base_managercCs|j}|s|js|jddD] }t|dr|jj}nq|r8z|j|WSty7td|j |fw|j r@|j dSdS)Nrrwrr) r+rVrxrrrwrDrrrYrB)rcr+rrrr2rGs*     zOptions.default_managercs<ddddddtdfdd |jd d DS) a1 Return a list of all forward fields on the model and its parents, excluding ManyToManyFields. Private API intended only to be used by Django itself; get_fields() combined with filtering of field properties is the public API for obtaining this field list. cS|jo|j Sr.rrErrrr2is_not_an_m2m_fieldz+Options.fields..is_not_an_m2m_fieldcSrr.)r one_to_manyrrrr2is_not_a_generic_relationrz1Options.fields..is_not_a_generic_relationcSs$|jo|jot|jdo|jj S)Nrx)r many_to_onerrrxrrrr2is_not_a_generic_foreign_keys z4Options.fields..is_not_a_generic_foreign_keyrAc3s,|]}|r|r|r|VqdSr.rrrrrrr2r3sz!Options.fields..Frr?rrhrrr2rAszOptions.fieldscCtddd|jDS)a Return a list of all concrete fields on the model and its parents. Private API intended only to be used by Django itself; get_fields() combined with filtering of field properties is the public API for obtaining this field list. rFcs|]}|jr|VqdSr.concreterrrr2r3rz*Options.concrete_fields..)r?rArhrrr2rF zOptions.concrete_fieldscCr)z Return a list of all concrete fields on the model. Private API intended only to be used by Django itself; get_fields() combined with filtering of field properties is the public API for obtaining this field list. rIcsrr.rrrrr2r3 rz0Options.local_concrete_fields..)r?rSrhrrr2rIrzOptions.local_concrete_fieldscCstddd|jddDS)a  Return a list of all many to many fields on the model and its parents. Private API intended only to be used by Django itself; get_fields() combined with filtering of field properties is the public API for obtaining this list. rEcss |] }|jr|jr|VqdSr.rrrrr2r3sz'Options.many_to_many..Frrrhrrr2rEs zOptions.many_to_manycCs$|jdddd}tddd|DS)aw Return all related objects pointing to the current model. The related objects can come from a one-to-one, one-to-many, or many-to-many field relation type. Private API intended only to be used by Django itself; get_fields() combined with filtering of field properties is the public API for obtaining this field list. FT)rrinclude_hiddenrLcss"|] }|jr |jjr|VqdSr.)hiddenrrE)r0rrrr2r3*s z*Options.related_objects..)rr?)rcall_related_fieldsrrr2rLs   zOptions.related_objectsc CsHi}|jdd}|D]}|||j<z|||j<Wq ty!Yq w|S)NFrrr=rrrcresrArrrr2rH-s   zOptions._forward_fields_mapc CsJi}|jddd}|D]}|||j<z|||j<Wq ty"Yq w|S)NFT)rrrrrrr2rJ<s  zOptions.fields_mapcCshz|j|WSty|jjstd|j|fYnwz|j|WSty3td|j|fw)zW Return a field instance given the name of a forward or reverse field. z%s has no field named '%s'. The app cache isn't ready yet, so if this is an auto-created related field, it won't be available yet.r)rHrr models_readyrrYrJ)rc field_namerrr2 get_fieldKs     zOptions.get_fieldcCsN|jsgS||jvr |gS|jD]}|j|}|r$|d||SqgS)z Return a list of parent classes leading to `model` (ordered from closest to most distant ancestor). This has to handle the case where `model` is a grandparent or even more distant relation. r)r`rwget_base_chaininsert)rcrxrrrrr2rds    zOptions.get_base_chaincCs8t|j}|jD]}|jD]}||qqt|S)z Return all the ancestors of this model as a list ordered by MRO. Useful for determining if something is an ancestor, regardless of lineage. )rr`rwget_parent_listrNr6)rcresultrancestorrrr2r us   zOptions.get_parent_listcCsF||jvr |j|S|jD]}|j|}|r |j|p|Sq dS)a\ Return the field on the current model which points to the given "ancestor". This is possible an indirect link (a pointer to a parent model, which points, eventually, to the ancestor). Used when constructing table joins for model inheritance. Return None if the model isn't an ancestor of this one. N)r`rwget_ancestor_link)rcr r parent_linkrrr2r s   zOptions.get_ancestor_linkc Cs||j|urgS|j}g}|}||D](}||ur|j}q|j|}|jf}|j}|t|jj|||ddddq|S)z Return a list of PathInfos containing the path from the current model to the parent model, or an empty list if parent is not a parent of the current model. FTN) from_optsto_opts target_fields join_fieldm2mdirectfiltered_relation) rxr_rrwr`rget_related_fieldrr )rcr proxied_modelpathopts int_model final_fieldtargetsrrr2get_path_to_parents*     zOptions.get_path_to_parentc Csz|j|urgS|j}|j|}|||g}t|ddD]\}}||d}|j|}|| q#|S)z Return a list of PathInfos containing the path from the parent model to the current model, or an empty list if parent is not a parent of the current model. Nr) rxr_rwrrrrr extendget_reverse_path_info) rcrrxchainrir childlinkrrr2get_path_from_parents     zOptions.get_path_from_parentc Cstt}|jjdd}|D]0}|j}|jrq dd|jdddD}|D]}t|jj t s<|jj jj jj }|| |q$q |D]}||jj jj }||jjd<q@|jdtS)a% This method is used by each model to find its reverse objects. As this method is very expensive and is accessed frequently (it looks up every field in a model, in every app), it is computed on first access and then is set as a property on every model. T)include_auto_createdcss$|] }|jr|jdur|VqdSr.)r related_modelrrrr2r3s z.F)rinclude_parentsrK)rr6r get_modelsrwrrr5rrxrr_rirr|rmEMPTY_RELATION_TREE) rcrelated_objects_graph all_modelsrxrfields_with_relationsr remote_labelrLrrr2!_populate_directed_relation_graphs& z)Options._populate_directed_relation_graphcCs|Sr.)r/rhrrr2rKszOptions._relation_treeTcCsX|r|jD] }||jvrt||q|r'|js'|jD] }||jvr&t||qi|_dSr.)FORWARD_PROPERTIESr|delattrrREVERSE_PROPERTIESrR)rcrr cache_keyrrr2rs        zOptions._expire_cachecCs|durt}|j||dS)a Return a list of fields associated to the model. By default, include forward and reverse fields, fields derived from inheritance, but not hidden fields. The returned fields can be changed using the parameters: - include_parents: include fields derived from inheritance - include_hidden: include fields that have a related_name that starts with a "+" F)r(r) PROXY_PARENTSr)rcr(rrrr2 get_fieldss zOptions.get_fieldsc CsP|ddtfvrtd|f|du}|rt}||j|||||f}z|j|WSty3Ynwg}|durp|jD]2} | |vrDq=| jj |j krP|tkrPq=| jj |||||dD]} t | ddri| j|j krn| | q[q=|r|j s|j} | D]} |s| jjs| | jqz|r||j7}||j7}|r||j7}td|}||j|<|S)a Internal helper function to return fields of the model. * If forward=True, then fields defined on this model are returned. * If reverse=True, then relations pointing to this model are returned. * If include_hidden=True, then fields with is_hidden=True are returned. * The include_parents argument toggles if fields from parent models should be included. It has three values: True, False, and PROXY_PARENTS. When set to PROXY_PARENTS, the call will return all fields defined for the current model or any of its parents in the parent chain to the model's concrete model. TFz(Invalid argument for include_parents: %sN)rrr(r seen_modelsrz get_fields())r4r7rrNrxrRrr`rwr_rrrr!rKrrrSrTrUr?) rcrrr(rr6 topmost_callr3rArr all_fieldsrrrr2r sT               zOptions._get_fieldscCr)z Return a list of total unique constraints. Useful for determining set of fields guaranteed to be unique for all rows. cSs$g|]}t|tr|jdur|qSr.)r5r condition)r0 constraintrrr2rfs z4Options.total_unique_constraints..)r-rhrrr2total_unique_constraints`sz Options.total_unique_constraintscCs>g}t|jD]}t|j|}t|tr||qt|S)zAReturn a set of the names of the properties defined on the model.)dirrxinspectgetattr_staticr5propertyr frozenset)rcnamesr=attrrrr2_property_namesls  zOptions._property_namescCsdd|jddtdDS)z Private API intended only to be used by Django itself. Fields to be returned after a database insert. cSsg|] }t|ddr|qS) db_returningF)rrrrr2r|s  z/Options.db_returning_fields..TF)rrr()rr4rhrrr2db_returning_fieldsvszOptions.db_returning_fieldsr.)F)TT)TF)TTTFN)4ryrrr0r2rrbrdr?rirkrnrorrrrrrrrrrrrrrrBrDrCrGrArFrIrErLrHrJrrr r rr%r/rKrr5rr;rCrErrrr2r@@s 9    = %         !       %    T   r@))rr}r= collectionsr django.appsr django.confrdjango.core.exceptionsrrrurdjango.db.modelsrr r r django.db.models.query_utilsr django.utils.datastructuresr rdjango.utils.functionalrdjango.utils.module_loadingrdjango.utils.textrrdjango.utils.translationrobjectr4r*r<rr:r?r@rrrr2s.