0% found this document useful (0 votes)
48 views

Django Notes

Django's ORM allows developers to write Python code using models that will be automatically translated to work with different database backends like PostgreSQL, MySQL, SQLite. Whenever models are created or modified, a two-step process is required - makemigrations to create migration files tracking changes, and migrate to execute those instructions and update the database. The Django admin interface provides a visual way to interact with and manage data, but models must be explicitly registered in the corresponding app's admin.py file to appear. Static files can be served from both the app static folders and additional locations configured in STATICFILES_DIRS.

Uploaded by

Kok Hung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Django Notes

Django's ORM allows developers to write Python code using models that will be automatically translated to work with different database backends like PostgreSQL, MySQL, SQLite. Whenever models are created or modified, a two-step process is required - makemigrations to create migration files tracking changes, and migrate to execute those instructions and update the database. The Django admin interface provides a visual way to interact with and manage data, but models must be explicitly registered in the corresponding app's admin.py file to appear. Static files can be served from both the app static folders and additional locations configured in STATICFILES_DIRS.

Uploaded by

Kok Hung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

DATABASE

Django ORM (Object-Relational Mapper), there is built-in support for


multiple database backends: PostgreSQL, MySQL, MariaDB, Oracle, or SQLite. This
means that
we, as developers, can write the same Python code in a models.py file and it will
automatically
be translated into each database correctly. The only configuration required is to
update the
DATABASES section of our config/settings.py file.

>> python manage.py migrate


create an initial database based on Django’s default
settings

migrate will sync the database with the current state of any database models
contained
in the project and listed in INSTALLED_APPS. In other words, to make sure the
database
reflects the current state of your project you’ll need to run migrate (and also
makemigrations)
each time you update a model.

Django’s ORM will automatically turn this model into a database table for us.

whenever we create
or modify an existing model we’ll need to update Django in a two-step process:
1. First, we create a migrations file with the makemigrations command. Migration
files create
a reference of any changes to the database models which means we can track changes–
and
debug errors as necessary–over time.
2. Second, we build the actual database with the migrate command which executes the
instructions in our migrations file.

ADMIN
provides a visual way
to interact with data

>> python manage.py createsuperuser

where is our posts app? It’s not displayed on the main admin page!
Just as we must explicitly add new apps to the INSTALLED_APPS config, so, too, must
we update
an app’s admin.py file for it to appear in the admin.

Model -> URL -> Views -> Template

STATIC
STATIC_URL: URL location of static files in our project
STATICFILES_DIR: we can tell Django where to look for static files beyond just
app/static folder

You might also like