(Python) Google App Engine DataSource에서 일대일에 합류하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1100472

  •  11-09-2019
  •  | 
  •  

문제

몇 가지 모델이 설정되어 있습니다.

class Apps(db.Model):
    name        = db.StringProperty(multiline=False)
    description = db.TextProperty()

class AppScreenshots(db.Model):
    image_file     = db.StringProperty(multiline=False)
    description    = db.StringProperty(multiline=False)
    app            = db.ReferenceProperty(Apps)

스크린 샷에서 "부모"앱을 참조하려고합니다.

a = Apps.get(app_key)   
ss = AppScreenshots(
    image_file     = 'foo',
    description    = 'bar',
    app            = a
)
ss.put()

그러나 그것은 나에게 다음과 같이 불평합니다.

BadArgumentError('_app should be a string; received ag1raWxsZXItcm9ib3RzcgoLEgRBcHBzGAkM (a Key):',)

나는 인터넷에서 몇 가지 예를 살펴 보려고했는데 모두 위와 같이 작동하는 것 같습니다. 하나 문서 세트 Google은 다음과 같이 조금 다르게 수행 할 것을 제안합니다.

a = Apps.get(app_key)   
ss = AppScreenshots(
    image_file     = 'foo',
    description    = 'bar',
    app            = a.key()
)
ss.put()

그러나 그것은 저에게 똑같은 오류를줍니다.

내가 뭘 잘못하고 있죠?

도움이 되었습니까?

해결책

코드를 실행하려고 할 때 발견 한 문제는 AppScreenshots의 'App'이름을 '앱'과 같은 다른 것으로 변경해야한다는 것입니다. '앱'이라는 단어는이 맥락에서 예약되어야합니다.

대신이 쿼리를 사용해보십시오. 첫 번째 엔티티를 원하지 않는다면 .filter ()도 할 수 있습니다.

class AppScreenshots(db.Model):
     image_file     = db.StringProperty()
     description    = db.StringProperty()
     apps            = db.ReferenceProperty(Apps)

appsObject = db.Query(Apps).get()

ss = AppScreenshots(image_file = 'foo', description = 'bar',apps = appsObject)

다음은 모델링 관계에 대한 유용한 기사입니다 링크.

관련 질문도 있습니다 여기에

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top