设置每个用户的限制以查看django ORM中的帖子

时间:2017-02-14 12:28:38

标签: django django-orm

models.py:

import pywintypes
import win32gui

displays = [[-10,0,980,530],
        [954,0,980,530],
        [-10,515,980,530],
        [954,515,980,530]] #these are the x1,y1,x2,y2 to corner all 4 videos on my res (1920x1080)

def enumHandler(hwnd, lParam):
    if win32gui.IsWindowVisible(hwnd):
        print(win32gui.GetWindowText(hwnd)) #this will print all the processes title
        if name in win32gui.GetWindowText(hwnd): #it checks if the process I'm looking for is running
            win32gui.MoveWindow(hwnd,i0,i1,i2,i3,True) #resizes and moves the process

win32gui.EnumWindows(enumHandler, None) #this is how to run enumHandler

我想获得每个用户每天只显示3个或更少帖子的最新帖子,例如:

class Post(models.Model):
    author = models.ForeignKey('User')
    text = models.TextField(max_length=320)
    created_date = models.DateTimeField(default=timezone.now)

我想要一个Django ORM命令,限制每个用户每天只显示3个帖子。 此查询应该有6个帖子:

User1 published 3 posts today.
User2 published 20 posts today.

1 个答案:

答案 0 :(得分:0)

可能不是最佳解决方案,但是这样的事情应该达到你想要做的目的。

posts = []
for user in User.objects.all():
    posts.append(Post.objects.filter(author=user).order_by('-created_date')[:3])