ReferenceProperty存储的是一个key,尝试获取它的值时,会自动访问datastore来get。因此你的做法会访问多次datastore。

get_value_for_datastore就是用来直接获取内部存储的值,也就是key的,因此你可以用它获取所有的key,再用db.get一次拿到。

前面写错了一点,完整的代码应该是这样:

db.get(set( [SecondModel.reference.get_value_for_datastore(s) for s in
secList] ))

还有不明白的可以看这里:
http://www.keakon.cn/bbs/thread-1959-1-1.html

----------
keakon



2010/10/25 saintthor <[email protected]>:
> 谢谢。get_value_for_datastore这个函数我没看懂是什么意思。但我要取的是被secList中元素引用的FirstModel的
> 集合,或许应该是
>
> set( [FirstModel.get_value_for_datastore(s.reference) for s in
> secList] )
>
> On 10月23日, 上午12时13分, 风笑雪 <[email protected]> wrote:
>> The entities didn't define a cmp opreation, they'll compared by their
>> memory address.
>>
>> You can try:
>>   set( [s.reference.key() for s in secList] )
>>
>> or more efficient way:
>>   set( [SecondModel.get_value_for_datastore(s) for s in secList] )
>>
>> ----------
>> keakon
>>
>> On Fri, Oct 22, 2010 at 4:00 PM,saintthor<[email protected]> wrote:
>> > class FirstModel(db.Model):
>> >    prop = db.IntegerProperty()
>>
>> > class SecondModel(db.Model):
>> >    reference = db.ReferenceProperty(FirstModel)
>>
>> > first1 = FirstModel(...).put()
>> > first2 = FirstModel(...).put()
>> > first3 = FirstModel(...).put()
>>
>> > fList = [first1,first2,first3]
>>
>> > sList = []
>> > for i in range( 10 ):
>> >  sList.append( SecondModel( reference = fList[i%3] ).put() )
>>
>> > # now, i have 10 SecondModel references 3 FirstModel.
>>
>> > q = SecondModel.all()
>>
>> > secList = q.fetch( q.count() )
>>
>> > FirstSet = set( [s.reference for s in secList] )
>>
>> > print len( FirstSet )
>>
>> > # now, len( FirstSet ) should be 3, but there is 10.
>>
>> > it regards same FirstModel referenced by different SecondModel as
>> > different. how to make it regards it same?
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups 
>> > "Google App Engine" group.
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to 
>> > [email protected].
>> > For more options, visit this group 
>> > athttp://groups.google.com/group/google-appengine?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Google App Engine" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-appengine?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to