在 Flask 中使用 Mongoengine 时遇到的奇怪问题

2014-01-17 12:34:27 +08:00
 guoqiao
这里 Post 表示一篇博客.
related_posts 表示与之相关的其它博客, 目前会返回10篇.
我想把这10篇中的前5篇切片到 x0, 后5篇切片到 x1.
但是给 x1 做完切片后, 发现 x0 也跟着变了, 这是为什么?

>>> from app.models import *
>>> p = Post.objects.get(date_slug='140117-2')
>>> p
<Post: Look in the mirror...redo via 'll'>
>>> x = p.related_posts()
>>> x
[<Post: JJ got a Hart!>, <Post: response to a poem I found >, <Post: RED Hair all the way!>, <Post: Cameron scared of UKIP>, <Post: THE RED HEADED STEPCHILD>, <Post: Are You A Sneaky Person.....????>, <Post: Arisen from the ASH>, <Post: michelle with two 'll' s>, <Post: Webster Rogets, advice on writing >, <Post: Pried then Lied>]
>>> x0 = x[0:5]
>>> x0
[<Post: JJ got a Hart!>, <Post: response to a poem I found >, <Post: RED Hair all the way!>, <Post: Cameron scared of UKIP>, <Post: THE RED HEADED STEPCHILD>]
>>> x1 = x[5:10]
>>> x1
[<Post: Are You A Sneaky Person.....????>, <Post: Arisen from the ASH>, <Post: michelle with two 'll' s>, <Post: Webster Rogets, advice on writing >, <Post: Pried then Lied>]
>>> x0
[<Post: Are You A Sneaky Person.....????>, <Post: Arisen from the ASH>, <Post: michelle with two 'll' s>, <Post: Webster Rogets, advice on writing >, <Post: Pried then Lied>]
>>>
3889 次点击
所在节点    Python
8 条回复
guoqiao
2014-01-17 12:41:32 +08:00
补充: 刚发现, 连 x 都变了:
>>> x
[<Post: Are You A Sneaky Person.....????>, <Post: Arisen from the ASH>, <Post: michelle with two 'll' s>, <Post: Webster Rogets, advice on writing >, <Post: Pried then Lied>]
fanzeyi
2014-01-17 12:45:00 +08:00
fanzeyi
2014-01-17 12:52:01 +08:00
啊不对,没看到是 Related ,查了下没看到 mongoengine 有提供 related_posts 这类函数,怎么实现的?
guoqiao
2014-01-17 12:57:13 +08:00
@fanzeyi 那个 Post 是我的一个 model, related_posts是我给它加的函数, 里面就是查询了一下, 返回了一些结果. 从你给的代码, 我没觉得我这样写有什么问题啊?
fanzeyi
2014-01-17 13:02:20 +08:00
@guoqiao 所以你不贴 related_posts 里怎么写的…… 怎么查原因啊- -
guoqiao
2014-01-17 13:07:42 +08:00
@fanzeyi 只是一个 queryset 而已:

def related_posts(self):
return Post.objects(topics__in=self.topics,headline__ne=self.headline,
is_spam__ne=True, is_draft__ne=True)[:10]
fanzeyi
2014-01-18 18:25:09 +08:00
@guoqiao 问题最终解决了么?

从代码上看毫无问题...
guoqiao
2014-01-19 10:29:34 +08:00
@fanzeyi 这个问题很诡异, 在我本地正常, 但是在服务器上就会出现上面的怪异结果. 难道是 mongoengine 版本的问题? 目前还没有解决.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/97370

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX