V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
guoqiao
V2EX  ›  Python

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

  •  
  •   guoqiao · 2014-01-17 12:34:27 +08:00 · 3865 次点击
    这是一个创建于 3946 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这里 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>]
    >>>
    8 条回复    1970-01-01 08:00:00 +08:00
    guoqiao
        1
    guoqiao  
    OP
       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
        3
    fanzeyi  
       2014-01-17 12:52:01 +08:00
    啊不对,没看到是 Related ,查了下没看到 mongoengine 有提供 related_posts 这类函数,怎么实现的?
    guoqiao
        4
    guoqiao  
    OP
       2014-01-17 12:57:13 +08:00
    @fanzeyi 那个 Post 是我的一个 model, related_posts是我给它加的函数, 里面就是查询了一下, 返回了一些结果. 从你给的代码, 我没觉得我这样写有什么问题啊?
    fanzeyi
        5
    fanzeyi  
       2014-01-17 13:02:20 +08:00
    @guoqiao 所以你不贴 related_posts 里怎么写的…… 怎么查原因啊- -
    guoqiao
        6
    guoqiao  
    OP
       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
        7
    fanzeyi  
       2014-01-18 18:25:09 +08:00
    @guoqiao 问题最终解决了么?

    从代码上看毫无问题...
    guoqiao
        8
    guoqiao  
    OP
       2014-01-19 10:29:34 +08:00
    @fanzeyi 这个问题很诡异, 在我本地正常, 但是在服务器上就会出现上面的怪异结果. 难道是 mongoengine 版本的问题? 目前还没有解决.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1072 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 19:26 · PVG 03:26 · LAX 11:26 · JFK 14:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.