Get the number of rows in a database

How is it possible to get the number of rows from a database?

If I use this sentence:

len(order.objects.all())

But my question is: Is it an optimized way to do it? Because I am fetching the entire base order to see its number of rows

This is the model:

class order (models.Model):
    STATUS = [
        ("RE","RECEIVED"),
        ("AC","ACCEPTED"),
    ]

    client = models.ForeignKey(client, on_delete=models.CASCADE)
    proveedor = models.ForeignKey (Proveedor, on_delete=models.CASCADE)
   
    ticket =  models.IntegerField()
    status = models.CharField(max_length=2,choices= STATUS,default= "SO")
       
    location_lat = models.FloatField()
    location_long = models.FloatField()
    day = models.DateField()
    time = models.TimeField()
   
    problem_description = models.TextField()
    picture1=models.ImageField(default=None, blank=True)
    picture2=models.ImageField(default=None,blank=True)

See the count() method.
https://docs.djangoproject.com/en/3.2/ref/models/querysets/#count

1 Like