66👍
✅
Try this code:
Message_me = Messages.objects.filter(username='myname', status=0).count()
6👍
You can either use Python’s len()
or use the count()
method on any queryset depending on your requirements. Also note, using len()
will evaluate the queryset so it’s always feasible to use the provided count()
method.
It can be used as follows:
message_count = models.Messages.objects.filter(username='username', status=0).count()
Alternatively, (if you do not worry about performance) you can also use len()
:
message_count = len(models.Messages.objects.filter(username='username', status=0))
You should also go through the QuerySet API Documentation for more information.
- [Django]-Django : Serving static files through nginx
- [Django]-Django Unique Together (with foreign keys)
- [Django]-Django – Website Home Page
0👍
To get the count of you can use the Model
// In models.py
class A(models.Model):
name = models.CharField(max_length=200)
// In views.py
from .models import A
def index(View):
c = A.objects.filter(username='myname', status=0).count()
print c // This will give you the count of the rows
👤Amit
- [Django]-Type object 'X' has no attribute 'objects'
- [Django]-PyCharm Not Properly Recognizing Requirements – Python, Django
- [Django]-Django REST Framework (DRF): Set current user id as field value
Source:stackexchange.com