Search code examples
djangodjango-modelsdjango-database

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


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')

Solution

  • Try this code:

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