gpt4 book ai didi

python - 由于 cursor() 方法中 "IN filter"的限制,使用游标进行分页查询会导致错误...应该有什么替代方法?

转载 作者:行者123 更新时间:2023-11-28 23:05:29 24 4
gpt4 key购买 nike

我正在使用以下模型开发一个类似 Twitter 的微博系统:

class Member(db.Model):    
user = db.UserProperty(required=True)
follower_count = db.IntegerProperty(default=0) # members following you
following_count = db.IntegerProperty(default=0) # members you are following

class NewsItem(db.Model):
text = db.StringProperty(required=True)
posted_by = db.ReferenceProperty(reference_class=Member,required=True,collection_name="posted_items")
posted_on = db.DateTimeProperty(auto_now_add=True)
status = db.IntegerProperty(default=1) # 0: deleted

class Follow(db.Model):
member = db.ReferenceProperty(reference_class=Member,required=True,collection_name="followings")
followed_member = db.ReferenceProperty(reference_class=Member,required=True,collection_name="followers")
added_on = db.DateTimeProperty(auto_now_add=True)

在此模型结构中,我使用以下代码检索当前用户关注的成员的消息:

follow_log_list = Follow.gql('WHERE member = :1 ', member)
followed_member_list = []
for follow_log in follow_log_list:
followed_member_list.append(follow_log.followed_member)

query = NewsItem.all()
query.filter('posted_by IN', followed_member_list)
query.filter('status =', 1)
query.order('-posted_on')
query.with_cursor(cursor)
newsList = query.fetch(10)

template_values['cursor'] = query.cursor()

调用 query.cursor() 方法时出现以下错误:

"No cursor available for a MultiQuery (queries using "IN" or "!=" operators)"

这是正常的,因为在游标的文档中,此限制已明确说明为:

http://code.google.com/appengine/docs/python/datastore/queries.html

“您不能将游标用于使用 IN 或 != 过滤运算符的查询。”

获取关注成员的帖子的替代方法是什么?

谢谢,

编辑:发布的消息按其状态过滤并按发布日期排序...但示例未在此处显示,我已经更改...

最佳答案

一种快速而讨厌的方式...

下载此 pagintor.py将其导入您的项目。

然后你可以为分页做这样的事情

    from paginator import Paginator, InvalidPage, EmptyPage
model = Member.all().fetch(100)
paginator = Paginator(model,5)

if(self.request.GET):
page = int(self.request.GET.get('page', '1'))
if(page is not None):
try:
page = int(self.request.GET.get('page', '1'))
except ValueError:
page = 1

# If page request (9999) is out of range, deliver last page of results.
try:
paginator = paginator.page(page)
except (EmptyPage, InvalidPage):
paginator = paginator.page(paginator.num_pages)
return self.response.out.write( template.render(path+'.html',{'paginator':paginator}))


#In templates

{% if paginator.object_list %}

{% for values in paginator.object_list %}

#do your tasks

{% endfor %}
<div align="right" class="pagination" >
{% if paginator.has_previous %}
<a id="previous" href="{{ paginator.previous_page_number }}">Previous</a>
{% else %}
<span class="page-nulled" >
Previous
</span>
{% endif %}

<span class="current" id="pagenum" title="{{ paginator.number }}">
&nbsp;&nbsp;&nbsp;Page {{ paginator.number }} of {{paginator.paginator.num_pages }}&nbsp;&nbsp;&nbsp;
</span>

{% if paginator.has_next %}
<a id="next" href="{{ paginator.next_page_number }}"> Next </a>
{% else %}
<span class="page-nulled" >
Next
</span>
{% endif %}

</div>

点击 next 或 previous 时,获取 href val() 并作为 get 变量传递给 url,例如 http://someurl?page= .

更多引用 here

关于python - 由于 cursor() 方法中 "IN filter"的限制,使用游标进行分页查询会导致错误...应该有什么替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6014657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com