Python 2.7 telah mencapai akhir dukungan
dan akan
dihentikan penggunaannya
pada 31 Januari 2026. Setelah penghentian penggunaan, Anda tidak akan dapat men-deploy aplikasi Python 2.7, meskipun organisasi Anda sebelumnya menggunakan kebijakan organisasi untuk mengaktifkan kembali deployment runtime lama. Aplikasi Python 2.7 yang ada akan terus berjalan dan menerima traffic setelah
tanggal penghentiannya. Sebaiknya Anda
bermigrasi ke versi Python terbaru yang didukung.
Class Kunci
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Catatan:
Developer yang membuat aplikasi baru sangat dianjurkan untuk menggunakan
Library Klien NDB, yang memiliki beberapa manfaat
dibandingkan dengan library klien ini, seperti menyimpan entity dalam cache secara otomatis melalui Memcache API. Jika saat ini Anda menggunakan Library Klien DB yang lebih lama, baca
Panduan Migrasi DB ke NDB
Instance Class kunci merepresentasikan kunci unik untuk entity Datastore.
Key
disediakan oleh modul google.appengine.ext.db
.
Pengantar
Setiap instance model memiliki kunci ID, yang mencakup jenis entity instance serta ID unik. ID dapat berupa string nama kunci, yang ditetapkan secara eksplisit oleh aplikasi saat instance dibuat, atau ID numerik bilangan bulat, yang ditetapkan secara otomatis oleh App Engine saat instance ditulis (put) ke Datastore. Metode
key()
instance model menampilkan objek Key
untuk instance tersebut. Jika instance belum diberi kunci, key()
akan memunculkan
NotSavedError
.
Aplikasi dapat mengambil instance model untuk Kunci tertentu menggunakan fungsi get().
Instance kunci dapat berupa nilai untuk properti entity Datastore, termasuk properti dinamis Expando dan anggota ListProperty. Model ReferenceProperty menyediakan fitur untuk nilai properti Kunci seperti dereferensi otomatis.
Konstruktor
- class Key(encoded=None)
-
Kunci unik untuk objek Datastore.
Kunci dapat dikonversi menjadi string dengan meneruskan objek Kunci ke
str()
.
String ini bersifat "urlsafe"—string ini hanya menggunakan karakter yang valid
untuk digunakan dalam URL. Representasi string
kunci ini dapat dikonversi kembali ke objek Kunci dengan
meneruskannya ke konstruktor Kunci
(argumen dienkode).
Catatan: Representasi string dari sebuah kunci akan tampak
samar, tetapi tidak
dienkripsi! File ini dapat dikonversi kembali menjadi data kunci mentah, baik jenis maupun
ID. Jika tidak ingin mengekspos data ini kepada pengguna (dan memungkinkan
mereka menebak kunci entity lain dengan mudah), enkripsi string ini
atau gunakan string lain.
- encoded
- Bentuk
str
dari instance Kunci untuk dikonversi kembali menjadi Kunci.
Metode Class
Class kunci menyediakan metode class berikut:
- Key.from_path(*path, parent=None, namespace=None)
-
Membuat objek Kunci baru dari jalur ancestor (opsional) (dalam objek Kunci yang ada), dan satu atau beberapa komponen jalur baru. Setiap komponen
jalur terdiri dari Nama jenis (kind
) dan ID (id_or_name
), yang berupa angka atau string karakter.
Jalur mewakili hierarki hubungan induk-turunan untuk suatu entity. Setiap entity dalam jalur diwakili oleh jenis entity, dan ID numerik atau nama kuncinya. Jalur lengkap mewakili entity yang muncul terakhir di jalur, dengan ancestor-nya (induk) sebagai entity sebelumnya.
Misalnya, panggilan berikut akan membuat Kunci untuk entity jenis
Address
dengan ID numerik 9876
, di bawah kunci induk User/Boris
:
k = Key.from_path('User', 'Boris', 'Address', 9876)
Cara alternatif untuk membuat Kunci yang sama adalah sebagai berikut:
p1 = Key.from_path('User', 'Boris')
k = Key.from_path('Address', 9876, parent=p1)
Untuk mengetahui informasi selengkapnya tentang jalur, lihat halaman Entity, Properti, dan Kunci.
Argumen
- path
- Daftar berisi satu atau beberapa komponen jalur ancestor, dengan setiap komponen terdiri dari jenis dan ID:
- jenis — Jenis entity, yang direpresentasikan sebagai string atau string Unicode.
- ID —
id
, ditentukan sebagai string atau panjang. Tidak boleh berupa angka 0.
- namespace=None
- Namespace yang akan ditetapkan untuk Kunci ini saja. Jika Anda menyediakan namespace di sini, namespace yang saat ini ditetapkan di namespace_manager akan diganti. Jika
None
, API akan menggunakan namespace dari namespace_manager.get_namespace saat ini.
- parent=None
- Tombol induk opsional. Jika tidak diberikan, setelan defaultnya adalah
None
.
Metode Instance
Instance kunci memiliki metode berikut:
- app()
-
Menampilkan nama aplikasi yang menyimpan entity data.
- has_id_or_name()
-
Menampilkan True
jika entity memiliki nama atau ID numerik.
- id()
-
Menampilkan ID numerik entity data, sebagai bilangan bulat, atau None
jika entity tidak memiliki ID numerik.
- id_or_name()
-
Menampilkan nama atau ID numerik entity data, mana pun yang dimilikinya, atau None
jika entity tersebut tidak memiliki nama atau ID numerik.
- kind()
-
Menampilkan jenis entity data, sebagai string.
- name()
-
Menampilkan nama entity data, atau None
jika entity tidak memiliki nama.
- namespace()
-
Menampilkan namespace entity data. Jika entity tidak memiliki namespace terkini, metode ini akan menampilkan namespace yang saat ini ditetapkan dalam namespace_manager.
- parent()
-
Menampilkan Kunci induk entity dari entity data, atau None
jika entity tidak memiliki induk.
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-09-04 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 UTC."],[[["\u003cp\u003eThe Key class represents a unique identifier for a Datastore entity, encompassing its entity kind and a unique identifier which can be either a key name or a numeric ID.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers are encouraged to use the NDB Client Library instead of the older DB Client Library, as NDB offers improved features like automatic entity caching.\u003c/p\u003e\n"],["\u003cp\u003eKey objects can be converted to and from "urlsafe" string representations, although these strings are not encrypted and reveal the underlying key data.\u003c/p\u003e\n"],["\u003cp\u003eThe Key.from_path() class method allows the creation of new Key objects by specifying an optional ancestor path and one or more path components, defining the hierarchy of parent-child relationships.\u003c/p\u003e\n"],["\u003cp\u003eKey instances provide various methods such as kind(), id(), name(), namespace(), and parent() to access information about the data entity associated with that key.\u003c/p\u003e\n"]]],[],null,["# The Key Class\n\n**Note:**\nDevelopers building new applications are **strongly encouraged** to use the\n[NDB Client Library](/appengine/docs/legacy/standard/python/ndb), which has several benefits\ncompared to this client library, such as automatic entity caching via the Memcache\nAPI. If you are currently using the older DB Client Library, read the\n[DB to NDB Migration Guide](/appengine/docs/legacy/standard/python/ndb/db_to_ndb)\n\nAn instance of the Key class represents a unique key for a Datastore entity.\n\n`Key` is provided by the `google.appengine.ext.db` module.\n\nIntroduction\n------------\n\nEvery model instance has an identifying\n*[key](./#Kinds_keys_and_identifiers),*\nwhich includes the instance's\n*[entity kind](/appengine/docs/legacy/standard/python/datastore/entities#Kinds_and_identifiers)*\nalong with a unique *identifier.* The identifier may be either a *key name* string, assigned explicitly by the application when the instance is created, or an integer *numeric ID,* assigned automatically by App Engine when the instance is written\n([put](/appengine/docs/legacy/standard/python/datastore/modelclass#Model_put))\nto the Datastore. The model instance's\n[key()](/appengine/docs/legacy/standard/python/datastore/modelclass#Model_key)\nmethod returns the `Key` object for the instance. If the instance has not yet been assigned a key, `key()` raises a\n[NotSavedError](/appengine/docs/legacy/standard/python/datastore/exceptions#NotSavedError).\n\nAn application can retrieve a model instance for a given Key using the [get()](/appengine/docs/legacy/standard/python/datastore/functions#get) function.\n\nKey instances can be values for Datastore entity properties, including [Expando](/appengine/docs/legacy/standard/python/datastore/expandoclass) dynamic properties and [ListProperty](/appengine/docs/legacy/standard/python/datastore/typesandpropertyclasses#ListProperty) members. The [ReferenceProperty](/appengine/docs/legacy/standard/python/datastore/typesandpropertyclasses#ReferenceProperty) model provides features for Key property values such as automatic dereferencing.\n\nConstructor\n-----------\n\nclass Key(encoded=None)\n\n: A unique key for a Datastore object.\n\n A key can be converted to a string by passing the Key object to\n `str()`.\n The string is \"urlsafe\"---it uses only characters valid\n for use in URLs. The string representation of\n the key can be converted back to a Key object by\n passing it to the Key constructor (the\n encoded argument).\n\n **Note:** The string representation of a key looks\n cryptic, but is not\n encrypted! It can be converted back to the raw key data, both kind and\n identifier. If you don't want to expose this data to your users (and allow\n them to easily guess other entities' keys), then encrypt these strings\n or use something else.\n\n encoded\n : The `str` form of a Key instance to convert back into a Key.\n\nClass Methods\n-------------\n\nThe Key class provides the following class method:\n\nKey.from_path(\\*path, parent=None, namespace=None)\n\n: Builds a new Key object from an (optional) ancestor path (in an existing Key object), and one or more new path components. Each path\n component consists of a Kind name (`kind`) and an identifier (`id_or_name`), which is either a number or a character string.\n\n A path represents the hierarchy of parent-child relationships for an entity. Each entity in the path is represented by the entity's kind, and either its numeric ID or its key name. The full path represents the entity that appears last in the path, with its ancestors (parents) as preceding entities.\n\n For example, the following call creates a Key for an entity of kind\n `Address` with numeric ID `9876`, under the parent key `User/Boris`: \n\n ```\n k = Key.from_path('User', 'Boris', 'Address', 9876)\n ```\n\n An alternative way of creating the same Key is as follows: \n\n ```\n p1 = Key.from_path('User', 'Boris')\n k = Key.from_path('Address', 9876, parent=p1)\n ```\n\n For more information about paths, see the [Entities, Properties, and Keys](/appengine/docs/legacy/standard/python/datastore/entities#Ancestor_paths) page.\n\n Arguments\n\n path\n : A list of one or more ancestor path components, where each component consists of a kind and an identifier:\n\n - kind --- The entity kind, represented as a string or a Unicode string.\n - identifier --- The `id`, specified as a string or long. It cannot be the number 0.\n\n namespace=None\n : The namespace to set for this Key only. If you supply a namespace here, it overrides the current namespace set in the [namespace_manager](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/api/namespace_manager/namespace_manager). If `None`, the API uses the current namespace from [namespace_manager.get_namespace](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/api/namespace_manager/namespace_manager#get_namespace).\n\n parent=None\n : Optional parent key. If not supplied, defaults to `None`.\n\n### Instance Methods\n\nKey instances have the following methods:\n\napp()\n\n: Returns the name of the application that stored the data entity.\n\nhas_id_or_name()\n\n: Returns `True` if the entity has either a name or a numeric ID.\n\nid()\n\n: Returns the numeric ID of the data entity, as an integer, or `None` if the entity does not have a numeric ID.\n\nid_or_name()\n\n: Returns the name or numeric ID of the data entity, whichever it has, or `None` if the entity has neither a name nor a numeric ID.\n\nkind()\n\n: Returns the kind of the data entity, as a string.\n\nname()\n\n: Returns the name of the data entity, or `None` if the entity does not have a name.\n\nnamespace()\n\n: Returns the namespace of the data entity. If the entity does not have a current namespace, this method returns the current namespace set in the [namespace_manager](/appengine/docs/legacy/standard/python/multitenancy/functions).\n\nparent()\n\n: Returns the Key of the data entity's parent entity, or `None` if the entity has no parent."]]