在 Flask 中使用 Mongoengine 时遇到的奇怪问题 - 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

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

  •  
  •   guoqiao Jan 17, 2014 4423 views
    This topic created in 4498 days ago, the information mentioned may be changed or developed.
    这里 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 replies    1970-01-01 08:00:00 +08:00
    guoqiao
        1
    guoqiao  
    OP
       Jan 17, 2014
    补充: 刚发现, 连 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  
       Jan 17, 2014
    啊不对,没看到是 Related ,查了下没看到 mongoengine 有提供 related_posts 这类函数,怎么实现的?
    guoqiao
        4
    guoqiao  
    OP
       Jan 17, 201
    @fanzeyi 那个 Post 是我的一个 model, related_posts是我给它加的函数, 里面就是查询了一下, 返回了一些结果. 从你给的代码, 我没觉得我这样写有什么问题啊?
    fanzeyi
        5
    fanzeyi  
       Jan 17, 2014
    @guoqiao 所以你不贴 related_posts 里怎么写的…… 怎么查原因啊- -
    guoqiao
        6
    guoqiao  
    OP
       Jan 17, 2014
    @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  
       Jan 18, 2014
    @guoqiao 问题最终解决了么?

    从代码上看毫无问题...
    guoqiao
        8
    guoqiao  
    OP
       Jan 19, 2014
    @fanzeyi 这个问题很诡异, 在我本地正常, 但是在服务器上就会出现上面的怪异结果. 难道是 mongoengine 版本的问题? 目前还没有解决.
    About     Help     Advertise     Blog     API     FAQ     Solana     3074 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 39.8.5 42ms UTC 15:03 PVG 23:03 LAX 08:03 JFK 11:03
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86