-3

Being a newbie programmer, most of the programs I've written never saved or loaded any of their data, and the few that did were saved by serializing the classes and saving it in raw text files or binary forms.

I am now in the process of learning Python with Django and I have a fundamental lack of understanding how things work behind the scenes. In all of the programs I've written, I know that if I have a class A and the class holds a linked-list data member list then the list exists in the memory (heap/stack).

Now assume I write a very large Django application and I have 10,000 instances of A, where exactly are they saved? How do I control them? I guess holding a list of 10,000 instances isn't rational, but how do I manage what is being loaded to the memory of the application, and what is being accessed directly through the database?

I hope I'm clear, being a newbie I don't know the right definitions that describes that things I mean and makes it hard to communicate so please feel free to edit and correct me.

Quaker
  • 1,483
  • 3
  • 20
  • 36
  • I think answering this question might lead to writing a book. I recommend you start without a framework (ie, write a simple python program) and read about databases, and data structures – Alvaro Jul 01 '14 at 14:14
  • Just get started with django tutorial. I think that will clear alot of things up for you. – Odif Yltsaeb Jul 01 '14 at 14:29
  • @Alvaro Referencing to a book can be a perfect answer, I'm not looking for someone to teach me everything. – Quaker Jul 01 '14 at 14:33
  • @OdifYltsaeb I did the `polls` tutorial, it didn't help me understand those points. – Quaker Jul 01 '14 at 14:33
  • How come? Did you not store objects in database? load them out? that should answer questions like where are items saved, how you control them and how to load them? Now the amount of items loaded can be controlled by several means, but i doubt no user will ever want to see 10000 of anything, so that is not a problem. – Odif Yltsaeb Jul 01 '14 at 15:19
  • @OdifYltsaeb I did store the one question and two choices. I don't understand how this will work with 10K questions. That's my intention. – Quaker Jul 01 '14 at 15:35
  • @OdifYltsaeb, let's say I want to add another instance to those 10K, do I have to load the whole 10K container or can I just add it to the container stored in the db? – Quaker Jul 01 '14 at 15:49
  • There is no difference in between adding one object or 10k objects into database. Just optimizing - you should do 1 query not 10k queries. For that reasons there are bulk operations - both different database engines and django provide them. I think you should rather install some database engine like mysql or postgresql along with some program like phpmyadmin or phppgadmin. Then run your tutorial app again and see for yourself how data is stored. – Odif Yltsaeb Jul 01 '14 at 18:24
  • @OdifYltsaeb I sure will. Thanks! – Quaker Jul 02 '14 at 13:03
  • 1
    Django is not different from any other Python program. If you have 10000 instances of `A` class in a list they are in memory. If `A` is an ORM `Model` class then you may save those instances to the database. If you have 10000 rows in your database they don't exist in memory until you load them, by starting to iterate over the Django queryset. – Anentropic May 10 '16 at 12:27

2 Answers2

1

I'm gonna divert from what you are asking to recommend reading about data storage, data structures, tables, and such before using an ORM, such as the one provided with django.

If I understand correctly, you don't mean memory management, but persistence of the data. How is data stored in nowadays business apps? Mostly in databases.

You cand find hundreds of tutorials on this matter, and based on the Relational Database Modelling Systems Django supports, I would start by learning relational databases.

I found this article to be pretty "straightforward"

Feel free to ask any questions if you don't understand the concepts. Once you get a grasp of how databases work, you will understand what does Django do and how your models persist over time.

Alvaro
  • 11,797
  • 9
  • 40
  • 57
  • I'm a computer science student, so I have some data structures knowledge. I just don't understand how my data structures and the database relationship occurs. – Quaker Jul 01 '14 at 15:47
  • 1
    Ok, some explanation on how an ORM works then? Email me if you want: alvarotuso@hotmail.com – Alvaro Jul 01 '14 at 19:27
0

It seems like your question is directed towards memory management, such as accessing the actual memory space in which objects are saved. Generally this is avoided, as python should handle this all for you, and it is not critical that you as a programmer need to manipulate the actual memory space in which your objects are stored. See Accessing object memory address

Django supports the use of various databases such as SQLite, MySQL, and PostgreSQL; all of which have different back-end operations.

It may be worth your time exploring the Django documentation on databases if you have not done so already: https://docs.djangoproject.com/en/dev/ref/databases/

Community
  • 1
  • 1
The2ndSon
  • 307
  • 2
  • 7