...

Open source softwares - Django

Back to Course

Lesson Description


Lession - #994 Django-Models


SQLite Database

At the point when we made the Django project, we got a empty SQLite database. It was made in the myworld root folder.

Create Table (Model>

To make another table, we should make another model.

In the/members/ folder, open the models.py record. It is practically vacant as a matter of course, with just an import proclamation and a comment:

members/models.py:
from django.db import models

# Create your models here.


To add a Members table in our database, begin by making a Members class, and depict the table fields in it:

members/models.py:
from django.db import models

class Members(models.Model>
: firstname = models.CharField(max_length=255>
lastname = models.CharField(max_length=255>


The primary field, "firstname" is a Text field, and will contain the main name of the individuals.

The second field, "lastname" is likewise a Text field, with the members' last name.

Both "firstname" and "lastname" is set up to have a limit of 255 characters.

Then explore to the/myworld/folder and run this command:

py manage.py makemigrations members

Which will bring about this result:
Migrations for 'members':
  members\migrations\0001_initial.py
    - Create model Members

(myproject>
C:\Users\Your Name\myproject\myworld>

Django makes a document with any new changes and stores the record in the/migrations/ folder.

Next time you run py manage.py move Django will make and execute a SQL proclamation, in light of the substance of the new document in the migrations folder.

Run the move command:

py manage.py migrate

Which will bring about this result:
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, members, sessions
Running migrations:
  Applying members.0001_initial... OK

(myproject>
C:\Users\Your Name\myproject\myworld>

The SQL statement made from the model is:

CREATE TABLE "members_members" (
"id" INT NOT NULL PRIMARY KEY AUTOINCREMENT,
"firstname" varchar(255>
NOT NULL, "lastname" varchar(255>
NOT NULL>
;