Property 값으로 데이터 필터링 하기
Model의 query 클래스를 활용한다.
query에 필터를 추가하여 여러가지 조건을 적용 할 수 있다.
예를 통해 살펴 보자
# userid가 '123'인 데이터를 조회하고 싶은 경우
qry = Account.query()
qry = Account.query(Account.userid == 123)
rslt = qry.fetch()
# 두가지 이상 조건을 적용 하고 싶은 경우
qry = Account.query(Account.userid >= 123, Account.userid < 456, ...)
rslt = qry.fetch()
# query를 중첩하여 여러가지 조건을 적용 할 수도 있다.
qry = Account.query()
qry1 = qry.filter(Account.userid >= 123)
qry2 = qry1.filter(Account.userid < 456)
rslt = qry.fetch()
rslt1 = qry1.fetch()
rslt2 = qry2.fetch()
간단하다.
AND, OR 연산자
ndb.AND(조건1, 조건2, ...)
ndb.OR(조건1, 조건2, ...)
이런식으로 사용 할 수 있다.
# ndb.OR
qry = Account.query(ndb.OR(Account.userid == 123, Account.userid == 125, Account.userid == 456))
# ndb.AND
qry = Account.query(ndb.AND(Account.userid == 123, Account.username == 'Sandy'))
Repeated Property (List) 조회하기
repeated property의 경우 list 형태로 값을 저장 하기 때문에 여러가지 조회 형태가 있을 수 있다.
class Article(ndb.Model):
title = ndb.StringProperty()
contents = ndb.StringProperty()
stars = ndb.IntegerProperty()
tags = ndb.StringProperty(repeated = True)
"""
# Data 세팅
article = Article(title='Introduction to Python',
stars=5,
tags=['python'])
article.put()
article = Article(title='Perl + Python = Parrot',
stars=5,
tags=['python', 'perl'])
article.put()
article = Article(title='Introduction to Perl',
stars=3,
tags=['php', 'perl'])
article.put()
article = Article(title='Ruby + Python',
stars=4,
tags=['python', 'ruby'])
article.put()
article = Article(title='JRuby + Python',
stars=4,
tags=['python', 'jruby'])
article.put()
"""
# tags property에 'perl'이 입력된 데이터 검색
qry = Article.query(Article.tags == 'perl')
# 또는
# qry = Article.query(Article.tags.IN(['perl']))
rslt = qry.fetch()
# tags property에 'perl'과 'ruby'가 동시에 입력된 데이터 검색
qry2 = Article.query(Article.tags.IN(['perl', 'ruby']))
# 또는
# qry2 = Article.query(ndb.OR(Article.tags == 'perl', Article.tags == 'ruby'))
rslt2 = qry2.fetch()
"!=", "IN" 연산자
property != value 연산자는
ndb.OR(property > value, property < value)로 표현 할 수 있다.
property.IN([value1, value2, ...]) 연산자는
ndb.OR(property == value1, property == value2, ...)로 표현 할 수 있다.
정열하기
query 클래스의 order 함수를 이용하면 된다.
# 제목 오름차순 정열
qry = Article.query().order(Article.title)
# 제목 오름차순, 별점 내림차순
qry = Article.query().order(Article.title, -Article.stars)
order 함수를 이용하고 local 에서(테스트 서버) 테스트 하면 자동으로 index.yaml 파일이 생성 되는 것을 확인 할 수 있다.
index.yaml 파일은 index 정보를 담고 있다. 자세한 내용은 https://developers.google.com/appengine/docs/python/config/indexconfig 링크를 참조.
(자동으로 생성되니까 일단 신경쓰지 않는다.)
부모키로 조회하기
부모키가 있는 경우 이를 이용해 데이터를 조회 할 수 있다.
key = ndb.Key(BlogPost, 12345) qry = Comment.query(ancestor=key) #위에 나온 다른 조건들과 같이 사용 할 수도 있다. qry = Comment.query(Comment.tags == 'python', ancestor=key).order(Comment.date)
Structured Property로 조회하기
간단한 예를 통해 살펴보자
# city property가 'Amsterdam'인 Address를 갖는 Contact를 조회
qry = Contact.query(Contact.addresses.city == 'Amsterdam')
# city property가 'Amsterdam'인 Address, 또는 type property가 'work'인 Address를 갖는 Contact를 조회
qry = Contact.query(Contact.addresses.city == 'Amsterdam', Contact.addresses.type == 'work')
# city property가 'Amsterdam', type property가 'work'인 Address를 갖는 Contact를 조회
qry = Contact.query(Contact.addresses == Address(city = 'Amsterdam', type = 'work'))
GenericProperty(Expando)로 조회 하기
Expando의 경우 entity구조를 동적으로 생성하기 때문에 일반적인 Mine.bar == 'blah' 요런 조건들이 먹히질 않는다.
bar라는 property가 미리 정의되지 않았기 때문인데, 이런 경우 아래와 같은 방법으로 조회 할 수 있다.
qry = Mine.query(ndb.GenericProperty('bar') == 'blah')
'Dev. > Google App Engine' 카테고리의 다른 글
| Datastore - NDB API(Python) : Transaction (0) | 2013.09.25 |
|---|---|
| Datastore - NDB API(Python) : Property Type (0) | 2013.08.23 |
| Datastore - NDB API(Python) : Model Hooks 사용하기 (trigger 역할) (0) | 2013.08.22 |
| Datastore - NDB API(Python) : Entity의 구조를 정확히 모를때 (동적 Entity) (0) | 2013.08.21 |
| Datastore - NDB API(Python) : Key를 이용한 데이터 삭제 (0) | 2013.08.16 |