Django 3 - Let's make To-Do-List / Database

We're modifying some settings inside of mysite directory(mysite -> settings.py)

Telling Django that we have another application that need to be set up inside of the project.

PUT  'main.apps.MainConfig' in installed_apps 

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

The next thing we're going to do is to get into mysite directory, following this commend(into terminal)

"python manage.py migrate" 

we'll see these ones... 

"Running migrations:

  Applying contenttypes.0001_initial... OK

  Applying auth.0001_initial... OK

  Applying admin.0001_initial... OK

  Applying admin.0002_logentry_remove_auto_add... OK

  Applying admin.0003_logentry_add_action_flag_choices... OK

  Applying contenttypes.0002_remove_content_type_name... OK 

  Applying auth.0002_alter_permission_name_max_length... OK

  Applying auth.0003_alter_user_email_max_length... OK

  Applying auth.0004_alter_user_username_opts... OK

  Applying auth.0005_alter_user_last_login_null... OK

  Applying auth.0006_require_contenttypes_0002... OK

  Applying auth.0007_alter_validators_add_error_messages... OK "


Model: modeling information

main-> migrations -> model 

we're going to create "ToDoList" 

from django.db import models

# Create your models here.
class ToDoList(models.Model):
    name = models.CharField(max_length=200)

    def _str_(self):
        return self.name

It essentially means we created database object, which is called ToDoList and define some of the attributes and entries that each model ToDoList is going to have. 

whenever you create new attribute of the information over your model, class as variable and name the attribute ("name")  and fill what to be stored in your database. 


now we define another model, which is Item

class Item(models.Model):
    todolist = models.ForeignKey(ToDoList, on_delete=models.CASCADE)
    text = models.CharField(max_length=300)
    complete = models.BooleanField()

Item is lil different because it's actually related to To-do-List, we're going to have items as a part of our to-do-list. 

The reason we're doing this is because of CharField, which is a type of field that we could store the information, we actually don't know the type of field or Django doesn't know the type of field that To-do-List is . So we have to define the fact that we're going to use a foreign key in this case, a to-do list object when we create an item. On_delete is just saying if we delete to-do-list since all items exist on a to-do-list, we're going to have to delete all of these as well. That's what CASCADE does, which defining the fact that this has a special way of being removed. 


Text will be the equal to a character field as well. text = models.CharField(max_length=300)


and creating Bolean field  complete = models.BooleanField()

 


Followign Commend: 

C:\Users\j\Desktop\pythonwork\Newproject>python manage.py makemigrations main


you're going to see this 

Migrations for 'main':

  main\migrations\0001_initial.py

    - Create model ToDoList      

    - Create model Item


To apply this change; Following commend:

C:\Users\j\Desktop\pythonwork\Newproject>python manage.py migrate


Operations to perform:

  Apply all migrations: admin, auth, contenttypes, main, sessions

Running migrations:

  Applying main.0001_initial... OK


Now you can see the new file main-> migrants-> 0001_initial.py


Following the command again:
C:\Users\j\Desktop\pythonwork\Newproject>python manage.py shell
Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

, which allows me to add some things into database.

First step is going to import our models so that we can use some of the methods and we can use those models to create objects and store them in the database. 

>>> from main.models import Item, ToDoList 

(from main.models cuz we're not in that main directory )

Now we're going to create a to-do list and add it to the database. 

>>> t = ToDoList(name="My List")
(name can be different.. whatever you want)  and hit enter 

>>> t.save()                [save this into the database]


if you want to see all of object 

>>> ToDoList.objects.all()

<QuerySet [<ToDoList: ToDoList object (1)>]>


Popular posts from this blog

[Python] Dictionary

[Visual Design 2/3]

[JavaScript] For loop , Function