Django - What is Database ?

What is... Database? 

A database is an organized collection of structured information, or data, typically stored electronically in a computer system. A database is usually controlled by a database management system (DBMS).   (from https://www.oracle.com/ca-en/database/what-is-database/#:~:text=A%20database%20is%20an%20organized,database%20management%20system%20(DBMS).

So how do we utilize the database in Django ? 


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main.apps.MainConfig',
]

when you go to settings.py, you see INSTALLED_APPS, a lot of these apps have database sections so basically we have like a database prepped. It means we already have a couple of tables that are ready to be migrated like django builds out a user table for us and authentication. they already build out a table to store session ids like if a user is logged in. What happens is django creates these migrations and theses are basically like SQL commands prepped and are ready to be executed so they're ready to be activated but they haven't been triggered yet. 

To run, in command   "python manage.py migrate" , it's going to execute theses commands so we're going to see all migrations being applied and basically what just happened is that built out our database for us. 


Go to models.py 

Models 

we're going to create python classes. Wow The class that we create is going to represent the database table.

<Database Table>

ID / Title / DESC.

1 / ... / ...

2 / ... / ....

3/ ... / ...


class project(models.Model):

    title = models.CharField()

    description = models.TextField()

    id = models.UUIDField()

In this example let's say we had a project this would create a table in the database called projects. Now every single attribute inside of that python class would represent a column inside of the database. we have ID, Title, and Description. Every single time we create a new row in that table that would be like an instance of a class and that's going to add in the row so the table or the class name is the table name the attributes are the columns so this is what it's going to create it's a replication. it's a sense a model of a table that's why they're called models. 


First table we create is going to be our table for our room. 

class Room(models.Model):
    #host =
    #topic =
    name = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    #participants =
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

Room Capital "R", that is the tradition for class and we're going to inherit from models so this is what's going to change it a standard python class to an actual django model,  and .Model is with a capital M.

There is going to be different attributes here, first one is  host; like the actual user that's going to be connected until we work with. we also have topic, we also have name name = models.CharField charfield, which is a character field and this is simply a text field so I have to specify the attribute and what type of value is that attribute. at this point, the character field takes in the value of max_length or it needs this parameter. 

we have description = models.TextField , basically it's same as character field it's just going to be a bigger text field and what I want to do here is I want to specify a value of null and I'm going to set that to true. so by default null is set to false so if I don't set it to true, like setting to false, it means that database cannot have an instance of this model here models.TextField(null=True without this value having something in it. so it can't be blank. so set null to true means that it can be blank so saying "null is allowed" - so true. 

blank=True means when we run the save method like when we submit a form, that form can also be empty so we're going to set that to true 

#participants what is going to do is store all the users that are currently active in a room so if you comment in a room you are now a participant like in a discord server you get to join one 

updated = models.DateTimeField(auto_now=True) updated value , is going to take a snapshot of any time this model instance was updated so anytime we run the save method to update this model or this specific table or the item, it's going to take a time stamp. DateTimeField ; we want the date and the time that it happened and we're going to add auto_now=True , means every time the save method is called go ahead and take a timestamp. 

created = models.DateTimeField(auto_now_add=True) auto_now_add 

The difference between_auto_now_&__auto_now_add_is auto now takes a snapshot on every time we save this item, auto now add only takes a time stamp when we first save or create this instance. so if we save it multiple times, this value will never change, it will just be the initial time stamp whereas updated with auto now is going to take a time stamp every single time. 


--------------------------------

We're going to create a string representation of this room; 

    def __str__(self):
        return self.name

when we add a model to the database, the first thing we need to do is "make migrations" 

in commend, "python manage.py makemigrations"

then we're going to see new file on the list.  "0001_initial.py"


and type "python manage.py migrate"  then you'll see Apply all migrations: admin, auth, base, contenttypes, sessions; Running migrations: Applying base.0001_initial... OK


OK. when you go to 127.0.0.1/8000/admin

you're going to see admin panel so we're going to create this from the command prompt.

1. python manage.py create superuser 

2. Username:  whatever you want

3. Email address: your email address

4. Password

5. Password(again)

successful !


Go to 127.0.0.1/8000/admin  and Log in;  now we're able to work with the database. 


To make another data in the database; Go to main/admin.py 

from .models import Room
admin.site.register(Room)

 Now refresh the website, you see new folder.











Popular posts from this blog

[Python] Dictionary

[Visual Design 2/3]

[JavaScript] For loop , Function