我有这个查询et:
plist = UserProfile.objects.filter(q).order_by('-created_at')
模型是:
class UserProfile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=30, blank=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True)
#some more fields
def last_seen(self):
return cache.get('seen_%s' % self.user.username)
def online(self):
if self.last_seen():
now = datetime.datetime.now()
if now > self.last_seen() + datetime.timedelta(
seconds=settings.USER_ONLINE_TIMEOUT):
return False
else:
return True
我想要实现的是过滤掉plist
以仅包含在线用户。
我尝试了不同的技巧,例如:
for p in plist:
if p.online:
print 'profile onlnine \n', p.id
online_plist.append(p)
plist = online_plist
但它不起作用。非常感谢您的提示。
更新:here is如何设置在线用户缓存:
class ActiveUserMiddleware:
def process_request(self, request):
current_user = request.user
if request.user.is_authenticated():
now = datetime.datetime.now()
cache.set('seen_%s' % (current_user.username), now,
settings.USER_LASTSEEN_TIMEOUT)