Skip to content

Guided Exploration 03

mtulepkaliev edited this page Oct 4, 2023 · 1 revision

Technical Documentation Pertaining to GE03

Table of Contents

Superuser

Superuser is created by running

python manage.py createsuperuser

This creates a superuser that can be accessed thorought the django admin panel.

Administration Panel

The admin panel is where superusers can view and manage models and tables. The admin panel can be accessed by going to the url

localhost:8000/admin

and logging in with the superuser credentials.

THe admin panel shows all the models that are associated with the admin panel as defined in admin.py. The admin panel can be used to add, edit, and delete models and their associated data.

When modifying or creating a model, the admin panel will display a form that is automatically generated based on the model definition. The form enforces the constraints that are defined in the model.

Models

Models are djagno's represnentation of database tables, Each instance of a model represents one row in the database. The attributes of each instance are the columns of the database table.

Each "Object" that will represent a table must be a subclass of the django.db.models.Model class. Each atribute of that object to be stored in the database must be a type of field object that is defined in django.db.models.fields.

The models are defined in the models,py file.

In order to view, add, edit, and delete models in the admin panel, the models must be registered in the admin.py file by including the line

from .models import *model_name*

admin.site.register(*model_name*)

Relationships

Django supports many-to-one, many-to-many, and one-to-one relationships between models. These are implemented using

ForeignKey(*model the key points to*,*action on delete*)
ManyToManyField(*model the key points to*)
OneToOneField(*model the key points to*,*action on delete*)

respectively.

Within a specific instance of a model, the related model can be accessed by using the name of the relationship as an attribute of the instance. for example

Project1,portfolio

will return the portfolio model that is related to the Project1 model.

Migrations

Migrations are used to update the database schema to match the current models.

A new migration must be made every time you make changes in the model that you would like related to the database

A migration is created by running

python manage.py makemigrations

This creates a migration file in the migrations folder of the app.

The migration file specified the changes that will be made to the database.

The migration is then applied by running

python manage.py migrate

This applies the migration to the database.

Clone this wiki locally