Python 2.7 ha raggiunto la fine del supporto
e verrà
ritirato
il 31 gennaio 2026. Dopo il ritiro, non potrai eseguire il deployment di applicazioni Python 2.7, anche se la tua organizzazione ha utilizzato in precedenza un criterio dell'organizzazione per riattivare i deployment di runtime legacy. Le tue applicazioni Python 2.7 esistenti continueranno a essere eseguite e a ricevere traffico dopo la
data di ritiro. Ti consigliamo di
eseguire la migrazione all'ultima versione supportata di Python.
Recupero dei risultati delle query
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Dopo aver creato una query, puoi specificare una serie di opzioni di recupero per controllare ulteriormente i risultati restituiti.
Per ulteriori informazioni su come strutturare le query per la tua app, consulta le query Datastore.
Recupero di una singola entità
Per recuperare una sola entità corrispondente alla tua query, utilizza il metodo Query.get()
(o GqlQuery.get()
):
q = Person.all()
q.filter("last_name =", target_last_name)
result = q.get()
Viene restituito il primo risultato trovato nell'indice che corrisponde alla query.
Eseguire l'iterazione dei risultati delle query
Quando esegui l'iterazione dei risultati di una query utilizzando il metodo run()
di un oggetto Query
o GqlQuery
, Cloud Datastore recupera i risultati in batch. Per impostazione predefinita, ogni batch contiene 20 risultati, ma puoi modificare questo valore utilizzando il parametro batch_size
del metodo. Puoi continuare a eseguire l'iterazione dei risultati della query finché non vengono restituiti tutti o il tempo di attesa della richiesta scade.
Recupero di proprietà selezionate da un'entità
Per recuperare solo proprietà selezionate di un'entità anziché l'intera entità, utilizza una query di proiezione. Questo tipo di query viene eseguito più velocemente e costa meno di una che restituisce entità complete.
Analogamente, una query basata solo su chiavi consente di risparmiare tempo e risorse restituendo solo le chiavi delle entità a cui corrisponde, anziché le entità stesse. Per creare questo tipo di query, imposta keys_only=True
durante la costruzione dell'oggetto query:
q = Person.all(keys_only=True)
Impostare un limite per la query
Puoi specificare un limite per la query per controllare il numero massimo di risultati restituiti in un batch. L'esempio seguente recupera le cinque persone più alte da Cloud Datastore:
q = Person.all()
q.order("-height")
for p in q.run(limit=5):
print "%s %s, %d inches tall" % (p.first_name, p.last_name, p.height)
Passaggi successivi
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-09-04 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eYou can refine query results by using various retrieval options to control the data returned.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eQuery.get()\u003c/code\u003e method allows for the retrieval of a single entity that matches the query criteria.\u003c/p\u003e\n"],["\u003cp\u003eQuery results can be iterated through in batches, with the default batch size being 20, which can be adjusted with the \u003ccode\u003ebatch_size\u003c/code\u003e parameter.\u003c/p\u003e\n"],["\u003cp\u003eProjection queries allow you to retrieve only selected properties of an entity, enhancing query speed and efficiency, while keys-only queries only return the keys to the entities, saving time and resources.\u003c/p\u003e\n"],["\u003cp\u003eA limit can be set on queries to restrict the number of results returned in a single batch, useful for controlling the amount of data processed.\u003c/p\u003e\n"]]],[],null,["# Retrieving query results\n\nAfter constructing a query, you can specify a number of retrieval options to\nfurther control the results it returns.\nSee [datastore queries](/appengine/docs/legacy/standard/python/datastore/queries) for more information on structuring queries for your app.\n\nRetrieving a single entity\n--------------------------\n\n\u003cbr /\u003e\n\nTo retrieve just a single entity matching your query, use the method [`Query.get()`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_get) (or [`GqlQuery.get()`](/appengine/docs/legacy/standard/python/datastore/gqlqueryclass#GqlQuery_get)): \n\n q = Person.all()\n q.filter(\"last_name =\", target_last_name)\n\n result = q.get()\n\nThis returns the first result found in the index that matches the query.\n\n\nIterating through query results\n-------------------------------\n\nWhen iterating through the results of a query using the `run()` method of a [`Query`](/appengine/docs/legacy/standard/python/datastore/queryclass#Query_run) or [`GqlQuery`](/appengine/docs/legacy/standard/python/datastore/gqlqueryclass#GqlQuery_run) object, Cloud Datastore retrieves the results in batches. By default each batch contains 20 results, but you can change this value using the method's `batch_size` parameter. You can continue iterating through query results until all are returned or the request times out.\n\nRetrieving selected properties from an entity\n---------------------------------------------\n\nTo retrieve only selected properties of an entity rather than the entire entity, use a [*projection query*](/appengine/docs/legacy/standard/python/datastore/projectionqueries). This type of query runs faster and costs less than one that returns complete entities.\n\nSimilarly, a [*keys-only query*](/appengine/docs/legacy/standard/python/datastore/queries#keys-only_queries) saves time and resources by returning just the keys to the entities it matches, rather than the full entities themselves. To create this type of query, set `keys_only=True` when constructing the query object: \n\n q = Person.all(keys_only=True)\n\nSetting a limit for your query\n------------------------------\n\nYou can specify a *limit* for your query to control the maximum number of results returned in one batch. The following example retrieves the five tallest people from Cloud Datastore: \n\n q = Person.all()\n q.order(\"-height\")\n\n for p in q.run(limit=5):\n print \"%s %s, %d inches tall\" % (p.first_name, p.last_name, p.height)\n\nWhat's next?\n------------\n\n- Learn the [common restrictions](/appengine/docs/legacy/standard/python/datastore/query-restrictions) for queries on Cloud Datastore.\n- Learn about [query cursors](/appengine/docs/legacy/standard/python/datastore/query-cursors), which allow an application to retrieve a query's results in convenient batches.\n- [Understand data consistency](/appengine/docs/legacy/standard/python/datastore/data-consistency) and how data consistency works with different types of queries on Cloud Datastore.\n- Learn the [basic syntax and structure of queries](/appengine/docs/legacy/standard/python/datastore/queries) for Cloud Datastore."]]