How to count the number of rows in a database table in Django

Friend picture Friend · Mar 26, 2013 · Viewed 40.8k times · Source

I have database created by Django model, where status and id and username are the fields of the table Messages.

I need to count the number of rows where status value= 0 for a particular username.

This is my incomplete code:

Message_me = Messages.objects.get(username='myname')

Answer

Mikhail Chernykh picture Mikhail Chernykh · Mar 26, 2013

Try this code:

Message_me = Messages.objects.filter(username='myname', status=0).count()