Get all Django records created since last login

牧云@^-^@ 提交于 2019-12-12 03:23:37

问题


Using Django models, is there a filter I can use to select all records created after a certain date? I'm not sure what syntax or filter I should use to get all records newer than my last_login.

I wouldn't mind doing it with a Q advanced query, but the simpler the better!


回答1:


Actually, you don't need the current datetime if you want to select all records created after a certain date. Just use a filter with a lookup like: field_date__gte=last_login. It's not necessary to use range here. For example:

MyModel.objects.filter(my_model_date__gte=last_login)



回答2:


You can do this:

first, get the user last_login:

last_login = user.last_login

then you can filter your data as you want like this:

Entry.objects.filter(pub_date__range=(last_login, datetime.datetime.now()))

hope it helps...



来源:https://stackoverflow.com/questions/9493342/get-all-django-records-created-since-last-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!