如何从数据中排除此类UID,并仅提取自一年后未登录的用户

2023-02-06 14:08:12 发布

您现在位置:Python中文网/ 问答频道 /正文

 def handle(self, **options):
        last_month = datetime.today() - timedelta(days=30)
        clients = Client.objects.filter(lastConnection__gte=last_month).values_list('uid','lastConnection')
        #print list(clients)
        last_year = datetime.today() - timedelta(days = 365)
        uid_list = Client.objects.filter()
        last_year_clients = Client.objects.filter(lastConnection__gte=last_year,uid__starstwith="%0000000000000000000000000009897").values_list('uid','lastConnection')
        print list(last_year_clients)

这是处理程序,模块如下所示:

class Client(models.Model):
    uid = models.CharField(max_length=128)
    key = models.CharField(max_length=128)
    img = models.TextField()
    version = models.CharField(max_length=20)
    lastConnection = models.DateTimeField()
    role = models.CharField(max_length=128,default="")

    def __str__(self):
        return "%s"%self.uid

我想提取自一年以来未登录且ID以“u'%000000000000000000000'开头的用户” 基本上,我想排除这种类型的ID,并列出自一年以来未登录的用户

任何帮助都将不胜感激

谢谢


Tags: selfclientuidobjectsmodelsdeffilteryearlengthmaxlistclientslastcharfieldlastconnection
1条回答
网友
1楼 · 发布于 2023-02-06 14:08:12
last_year = datetime.today() - timedelta(days = 365)
        uid_list = Client.objects.filter()
        last_year_clients = Client.objects.filter(lastConnection__lte=last_year).exclude(uid__startswith="%00000").values_list('uid','lastConnection')
        with open("last_year.txt","wb") as fp:
                for s in last_year_clients:
                        fp.write(str(s)+"\n")

相关问题 更多 >