calculate the followers difference/grow by day

31 views
Skip to first unread message

João Paulo

unread,
May 31, 2018, 10:58:07 AM5/31/18
to Django users
 
Hey guys,

I´m using SQLite and trying to calculate the followers difference by day

This is my model:

class Statistics(models.Model):
    followers = models.IntegerField()
    last_update = models.DateTimeField(auto_now_add=True)

This are my database rows:

"6" "50" "2018-05-29 00:25:48.276102"
"1" "100" "2018-05-29 00:26:48.276102"
"3" "200" "2018-05-30 00:27:04.178444"
"5" "250" "2018-05-30 00:30:04.178444"
"4" "300" "2018-05-31 00:27:04.178444"

And the result I´m tryting to achieve is:

followers | last_update
150          2018-05-30
50           2018-05-31

# I´m new to Django and thinking about use a Manager, but don't know if´s doable using a Manager
# Does anyone has a suggestion ?

Daniel Germano Travieso

unread,
Jun 4, 2018, 2:25:37 PM6/4/18
to django...@googlegroups.com
Hello!
I don't quite understand what you mean by the followers difference by day, but as that difference by day is not a property of each model tuple, but a property of a specific day, you can write a view to handle that processing. Custom model Managers are used to either add manager methods or to customize and change the QuerySet your model returns. If you wish to use the model manager for this, it could be used to add a field to your model that makes a query to the last followers on that day and calculates the difference (?). You could also use a @property on your Model class for that too.

Hope it helps!

[]'s
Daniel Germano Travieso
Engenharia da Computação Turma: 013
Unicamp

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3196a628-a53f-41ac-bcbf-6cc8b8a66016%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Farris

unread,
Jun 5, 2018, 3:10:49 AM6/5/18
to django...@googlegroups.com
I think you can do something like this

stats = Statistics.objects.all().order_by('-last_update')
latest_stat = 0
for stat in stats:
print(abs(latest_stat - stat.followers))
latest_stat = stat.followers

Reply all
Reply to author
Forward
0 new messages