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
bb2018
V2EX  ›  Python

Python django 比如 用户 a, b,c 分别各写了几篇日记,怎么调用用户 a,b,c,以他们写日记的时间排序.

  •  
  •   bb2018 · 2017-08-12 14:45:50 +08:00 · 1776 次点击
    这是一个创建于 2449 天前的主题,其中的信息可能已经有所发展或是发生改变。
    class User(object):
    username=models.CharField()
    xxx
    pub_date = models.DateTimeField(auto_now_add=True)

    class Notes(models.Modle):
    author = models.ForeignKey(User)
    title = models.CharField()
    pub_date = models.DateTimeField(auto_now_add = True)
    body = models.TextField()

    两个模型,
    怎么调用 最新写过日记的用户.

    比如 用户 a, b,c 分别各写了几篇日记,怎么调用用户 a,b,c,以他们写日记的时间排序.
    5 条回复    2017-08-13 15:13:36 +08:00
    hefish
        1
    hefish  
       2017-08-12 20:28:15 +08:00
    当然是用 Model 里面提供的方法来调用啊。仔细看例子。
    bb2018
        2
    bb2018  
    OP
       2017-08-12 23:43:56 +08:00
    @hefish 上面是两个模型,应该在哪个模型下写?
    chengxiao
        3
    chengxiao  
       2017-08-13 09:33:38 +08:00
    notes = Notes.objects.filter(author=a,b,c).order_by(-'pub_date')
    chengxiao
        4
    chengxiao  
       2017-08-13 09:38:50 +08:00
    如果调用最新 10 篇用户文章 notes = Notes.objects.all().order_by(-‘ pub_date ’)[:10]
    bb2018
        5
    bb2018  
    OP
       2017-08-13 15:13:36 +08:00
    @chengxiao 这样调出来的是最新的日记吧?
    我的意思是,哪个用户刚刚写了日记,他的排名就在最前面,
    根据这个得到前5个或10个最新写了日记的用户.这样子
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5357 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 09:07 · PVG 17:07 · LAX 02:07 · JFK 05:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.