简体   繁体   中英

How does django handle getting data from the database?

I'm trying to reproduce something similiar to Djangos' Model class in PHP. I was wondering how django handles the loading and saving entries from a models' table.

For example, when calling Person.objects.all() ( Person being a subclass of django.db.models.Model of course), I expect Django to perform an SQL Query, SELECT * FROM myapp_person , for instance, and then transforms the data recieved from the query to instances of the Model-class. Is this correct? Then,

  1. Couldn't that lead to memory overflow when there are too many entries, or is there a way Django handles this side-effect?
  2. When calling Person.objects.filter(name="Paul") I expect Django to perform an SQL Query like SELECT * FROM myapp_person WHERE name = 'Paul' , but what if Person.objects.all() was called before? Does django cache them or does it just perform the request for each call?
  1. Django reads objects from database in portions, but makes a cache inside a QuerySet object. So if you have read whole data from queryset, memory will be used.
  2. All QuerySet methods which return QuerySets actually make a copy of it inside, and do not copy any cache in new object. So you can be sure that queryset always contains actual data, no matter if any of its "parents" or qs itself has been processed or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM