Python 2.7은 지원이 종료되었으며 2026년 1월 31일에 지원 중단됩니다. 지원 중단 후에는 조직에서 이전에 조직 정책을 사용하여 레거시 런타임의 배포를 다시 사용 설정한 경우에도 Python 2.7 애플리케이션을 배포할 수 없습니다. 기존 Python 2.7 애플리케이션은 지원 중단 날짜 이후에도 계속 실행되고 트래픽을 수신합니다. 지원되는 최신 Python 버전으로 마이그레이션하는 것이 좋습니다.
Key는 변경할 수 없는 데이터 저장소 키입니다. 애플리케이션은 일반적으로 항목을 참조하는 데 키를 사용합니다. 저장된 모든 항목에는 키가 있습니다. 항목의 키를 가져오려면 모델의 키 속성을 사용하세요.
이 키에서 항목을 가져오려면 Key 객체의 get() 메서드를 호출합니다.
키는 비교(예: key1 == key2 또는 key1 < key2)를 지원합니다.
이 연산자는 애플리케이션 ID, 네임스페이스, 전체 '상위 경로'를 비교하며, 키 속성 또는 키를 기준으로 정렬할 때 Datastore에서 쿼리에 사용하는 순서와 동일한 순서를 사용합니다.
repr(key) 또는 str(key)은 앱과 네임스페이스가 기본값과 다르지 않은 경우 이를 생략하고 가장 짧은 생성자 양식과 유사한 문자열 표현을 반환합니다.
hash(key)가 작동합니다. 이를 통해 해시 테이블에 키를 저장할 수 있습니다.
생성자
유연성과 편의성을 위해 다중 생성자 시그니처가 지원됩니다.
클래스Key(kind1, id1, kind2, id2, ...)
클래스Key(pairs=[(kind1, id1), (kind2, id2), ...])
클래스Key(flat=[kind1, id1, kind2, id2, ...])
클래스Key(urlsafe=string)
위치 인수 kind1, id1, kind2, id2...는 '상위' 순서입니다. flat=[kind1, id1,
kind2, id2]에 대한 바로가기이며 상위가 하위보다 앞섭니다.
위치 인수인 pairs와 flat 생성자 양식은 상위=key를 사용하여 다른 키를 추가로 전달할 수 있습니다. 상위 키 쌍 (kind, id)은 명시적으로 전달된 (kind, id) 쌍 앞에 삽입됩니다.
urlsafe 키워드 매개변수는 websafe-base64로 인코딩되어 직렬화된 참조를 사용합니다. 그러나 이 참조는 고유하고 불분명한 문자열로 여기는 것이 좋습니다.
다른 생성자 키워드 인수는 다음과 같습니다.
app
애플리케이션 ID(문자열)를 지정합니다.
namespace
네임스페이스(문자열)를 지정합니다.
Datastore에 영향을 미치지 않는 인스턴스 메서드
다음 메소드는 키 내용에 액세스하며 데이터 저장소 I/O 활동에는 관여하지 않습니다.
pairs()
(kind, id) 쌍의 튜플을 반환합니다.
flat()
평면화된 종류 및 id 값(kind1, id1, kind2, id2, ...)의 튜플을 반환합니다.
app()
애플리케이션 ID를 반환합니다.
id()
마지막 (kind, id) 쌍의 문자열 또는 정수 ID를 반환하거나 키가 불완전한 경우 None을 반환합니다.
string_id()
마지막 (kind, id) 쌍의 문자열 ID를 반환하거나, 키에 정수 ID가 있거나 불완전한 경우 None을 반환합니다.
integer_id()
마지막 (kind, id) 쌍의 정수 ID를 반환하거나, 키에 문자열 ID가 있거나 불완전한 경우 None을 반환합니다.
namespace()
네임스페이스를 반환합니다.
kind()
마지막 (kind, id) 쌍의 종류를 반환합니다.
parent()
마지막 (kind, id) 쌍에서 구성된 키를 반환하거나, 키에 (kind, id) 쌍 한 개만 있을 경우 None을 반환합니다.
urlsafe()
websafe-base64로 인코딩된 직렬 버전의 키를 반환합니다.
참고: URL 안전 문자열은 암호화된 듯 보이지만 아니기 때문에
쉽게 디코딩하여 원래 항목의 종류 및 식별자를 복구할 수 있습니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[[["\u003cp\u003eThis API for bundled services and \u003ccode\u003eKey\u003c/code\u003e class operations is specifically for first-generation runtimes in the App Engine standard environment, with migration to Python 3 requiring a specific guide.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eKey\u003c/code\u003e class represents an immutable Datastore key, used to refer to entities and to retrieve them using the \u003ccode\u003eget()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eKeys can be compared using operators such as \u003ccode\u003e==\u003c/code\u003e and \u003ccode\u003e<\u003c/code\u003e, and also support string representation, as well as being used within a hash table.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eKey\u003c/code\u003e class offers various constructor signatures, including those for ancestor order, pairs, flat structures, and URL-safe strings, along with options for specifying the application ID and namespace.\u003c/p\u003e\n"],["\u003cp\u003eThere are multiple methods to access key properties and contents, which do not affect the Datastore, as well as other methods that will interact with the Datastore, like to get or delete an entity.\u003c/p\u003e\n"]]],[],null,["# NDB Key Class\n\n| This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment. If you are updating to the App Engine Python 3 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\nAn instance of the `Key` class represents an immutable\nDatastore key.\n\nThis page has API reference documentation. For an overview,\nsee [NDB Entities and Keys](/appengine/docs/legacy/standard/python/ndb/creating-entities).\n\nIntroduction\n------------\n\nA Key is an immutable Datastore key. Applications normally use them to refer\nto entities. Any entity that has been stored has a key. To get an entity's\nkey, use the model's [key](/appengine/docs/legacy/standard/python/ndb/modelclass#Model_key)\nproperty.\nTo retrieve an entity from its key, call the `Key`\nobject's [get()](#Key_get) method.\n\nKeys support comparisons, for example\n\u003cvar translate=\"no\"\u003ekey1\u003c/var\u003e` == `\u003cvar translate=\"no\"\u003ekey2\u003c/var\u003e or\n\u003cvar translate=\"no\"\u003ekey1\u003c/var\u003e` \u003c `\u003cvar translate=\"no\"\u003ekey2\u003c/var\u003e.\nThese operators compare application ID, namespace, and the full\n\"ancestor path\". They use the same ordering as the Datastore uses\nfor queries when ordering by a key property or by key.\n\n`repr(`\u003cvar translate=\"no\"\u003ekey\u003c/var\u003e`)` or\n`str(`\u003cvar translate=\"no\"\u003ekey\u003c/var\u003e`)` returns\na string representation resembling\nthe shortest constructor form, omitting the app and namespace\nunless they differ from the default value.\n\n`hash(`\u003cvar translate=\"no\"\u003ekey\u003c/var\u003e`)` works. Thus, you can store keys in\na hash table.\n\nConstructors\n------------\n\nFor flexibility and convenience, multiple constructor signatures are\nsupported.\n\nclass Key(\u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid1\u003c/var\u003e, \u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid2\u003c/var\u003e, ...)\nclass Key(pairs=\\[(\u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid1\u003c/var\u003e), (\u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid2\u003c/var\u003e), ...\\])\nclass Key(flat=\\[\u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid1\u003c/var\u003e, \u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid2\u003c/var\u003e, ...\\])\nclass Key(urlsafe=\u003cvar translate=\"no\"\u003estring\u003c/var\u003e)\n\n: The positional arguments\n \u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid1\u003c/var\u003e, \u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid2\u003c/var\u003e...\n are in \"ancestor\" order. (This is a shortcut for\n `flat=[`\u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e`, `\u003cvar translate=\"no\"\u003eid1\u003c/var\u003e`,\n `\u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e`, `\u003cvar translate=\"no\"\u003eid2\u003c/var\u003e`]`) Parents come before children.\n\n The positional-arguments, `pairs`, and `flat`\n constructor forms can additionally pass in another\n key using parent=\u003cvar translate=\"no\"\u003ekey\u003c/var\u003e.\n The (\u003cvar translate=\"no\"\u003ekind\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid\u003c/var\u003e) pairs of the parent key are\n inserted before the (\u003cvar translate=\"no\"\u003ekind\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid\u003c/var\u003e) pairs\n passed explicitly.\n\n The `urlsafe` keyword parameter uses\n a websafe-base64-encoded serialized\n reference\n but it's best to think of it as just an opaque unique\n string.\n\n Additional constructor keyword arguments:\n\n app\n : specify the application id (a string)\n\n namespace\n : specify the namespace (a string)\n\nInstance Methods that Don't Affect the Datastore\n------------------------------------------------\n\nThe following methods access the contents of a key. They do not\nengage in any Datastore I/O activity.\n\npairs()\n\n: Returns a tuple of (kind, id) pairs.\n\nflat()\n\n: Returns a tuple of flattened kind and id values\n (kind1, id1, kind2, id2, ...).\n\napp()\n\n: Returns the application id.\n\nid()\n\n: Returns the string or integer id in the last (kind, id) pair,\n or `None` if the key is incomplete.\n\nstring_id()\n\n: Returns the string id in the last (kind, id) pair,\n or `None` if the key has an integer id or is incomplete.\n\ninteger_id()\n\n: Returns the integer id in the last (kind, id) pair,\n or `None` if the key has an string id or is incomplete.\n\nnamespace()\n\n: Returns the namespace.\n\nkind()\n\n: Returns the kind in the last (kind, id) pair.\n\nparent()\n\n: Returns a Key constructed from all but the last (kind, id) pair\n (or `None` if the key has just one (kind, id) pair).\n\nurlsafe()\n\n: Returns a websafe-base64-encoded serialized version of the key.\n\n **Note:**\n The URL-safe string looks cryptic, but it is not encrypted!\n It can easily be decoded to recover the original entity's kind and\n identifier.\n\nto_old_key()\n\n: Returns a\n [Key](/appengine/docs/legacy/standard/python/datastore/keyclass)\n for the \"old\" Datastore API (`db`).\n\nInstance Methods that Affect the Datastore\n------------------------------------------\n\nThese methods interact with the Datastore.\n\nget(\\*\\*ctx_options)\n\n: Returns the entity for the Key.\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)\n\nget_async(\\*\\*ctx_options)\n\n: Returns a\n [Future](/appengine/docs/legacy/standard/python/ndb/futureclass)\n whose eventual result is\n the entity for the Key.\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)\n\ndelete(\\*\\*ctx_options)\n\n: Delete the entity for the Key.\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)\n\ndelete_async(\\*\\*ctx_options)\n\n: Asynchronously delete the entity for the Key.\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)\n\nClass Methods\n-------------\n\nfrom_old_key(k)\n\n: Returns an NDB key from the passed in \"old\" Datastore API\n (`db`)\n [Key](/appengine/docs/legacy/standard/python/datastore/keyclass).\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)"]]