问题
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