capture_date is a DateField. That means that it’s not timezone aware. However, a “current_datetime” is likely going to be timezone aware. That means you may not be looking at the proper rows to get these counts.
F(‘project’) is going to retrieve the value of the project field of the model. It’s then going to compare that value to the project field of that same model.
They’re always going to be the same. That filter isn’t going to filter out any rows.
In the line:
Your reference to 'user_count' is incorrect here. This is a field within the model that you’re trying to access, and so you need to use the F function to identify it as such.
On the other hand, you’re annotating the user_count query with a value for user_count_yesterday, and then using that field for the annotation for difference. You could simply use your value for yesterday in the calculation for difference.
Finally, I think you’re trying to use the query defined for yesterday as a subquery - it needs to be written as such if you want this to be done as a single query. (Otherwise, what you have here are two separate queries - the first to get yesterday’s user_count and the second to calculate the difference.) It depends upon whether you’re trying to run this as one query for multiple projects, or if this is one query for one project. The intent is not clear here.
All I’m really after is to have the difference between yesterday’s and today’s users available within a single queryset so that I can present the data in a table showing all projects (project name, current count (value as of today)), and the difference. Basically just trying to show the increase or decrease of users day on day.
Does it make sense to do this all in a single query? The table for this particular view is going to show all projects, not just 1, but it might be that later on that it could be for a single project view.
Ignore that last post, I am going over the docs, trying different things and getting myself in a mess.
This is my current approach which I feel is getting closer to what I need, which is the Sum of user_count minus yesterdays user_count. I’d like this as a parameter delta within the queryset for every project.
I’ve taken this approach to setting the time zone to be consistent.
Great! So, the situation is that there’s no aggregation involved. There’s no need for the Sum function anywhere in this.
For the purposes of this discussion, I’m going to assume you’ve already figured out today and yesterday. So, for each Project, I want today’s user_count and today’s user_count - yesterday’s user_count.
This is going to be a query based on Usergrowth.
will give us today’s count (among the rest of the data).
will give yesterday’s count. This is what we’re going to (basically) use as the subquery. Additionally, we need to match this to the project in the “outer” query, which is where the OuterRef function comes into play. So the subquery is going to look like: Usergrowth.objects.filter(project=OuterRef('project'), capture_date=yesterday)
We then only want the user_count field, so we’ll use the values function to specify the data element to return.