I made a project in django, python.I would like to know Django command for knowing what's memory size of my table
Mixing both answers, you could calculate the size of the whole table calculating first the size of a single object of your model and then multiplying it with the number of objects you have in your table:
$ python manage.py shell
>>> import sys
>>> from myapp.models import MyModel
>>> my_model = MyModel()
>>> total_size = sys.getsizeof(my_model) * MyModel.objects.count()
This could work, but I think the right thing to do is to calculate the size of the table using the database. For example, if your using PostgreSQL, you just have to do:
SELECT pg_database_size('myapp_mymodel');
pg_database_size
------------------
63287944
(1 row)
from django.db import connection
with connection.cursor() as c:
c.execute("SELECT pg_size_pretty(pg_total_relation_size('my_table'))")
c.fetchall()
e.g. >> [('7403 MB',)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With