I have an action that a user can do many times a day. I’m trying to get a count of how many times the user has taken that action, but only for today’s date. Here’s the way I’m currently solving this, but is there an easier way? I feel like I should be able to fit this in one line. 🙂
today_slaps = 0
slaps = Slap.objects.filter(from_user=request.user.id)
for slap in slaps:
if slap.date.date() == datetime.now().date():
today_slaps += 1
The logic I’m looking for is:
slaps = Slap.objects.filter(from_user=2, date.date()=datetime.now().date()).count()
But that’s obviously throwing an error that a keyword can’t be an expression. Sorry if this is a basic one, but thoughts?
Generates the following SQL:
So it’s safe to say you will only get today’s slaps, again, unless you have slaps from the future. If you do, I’d set every
date__day, date__year, date__monthexplicitly.