You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Django ORM (Object-Relational Mapping) allows us to define db schemas as Python classes
fromdjango.dbimportmodelsclassPost(models.Model):
title=models.CharField(max_length=100) # short textcontent=models.TextField() # large textcreated_at=models.DateTimeField(auto_now_add=True) # date field
Database field types
Field Types for Data Representation
# texttitle=models.Charfield(max_length=255) # short textdescription=models.TextField() # long text# numberage=models.IntegerField()
price=models.FloatField()
price_detailed=models.DecimalField(max_digits=10, decimal_places=2) # precise decimal numbers# booleanis_active=models.BooleanField(default=True)
# datebirth_rate=models.DateField() # dataappointment_time=models.TimeField() # timecreated_at=models.DateTimeField() # data and time# file and imagedocument=models.FileField(upload_to='documents/')
profile_picture=models.ImageField(upload_to='images/')
Field Types for Relationships
author=models.Foreignkey(User, on_delete=models.CASCADE) # many to oneprofile=models.OneToOneField(User, on_delete=models.CASCADE) # one to onetags=models.ManyToManyField(Tag) # many to many
# Create Django project
django-admin startproject project_name
# Run the development server# python path_to/manage.py runserver
python manage.py runserver # default path -> http://127.0.0.1:8000/# Run migrations
python manage.py migrate
# Create an app
python manage.py startapp app_name
Creating an App
In Django a project is a collection of configurations and settings for your entire application, while an app is a modular component that performs a specific function or feature within the project
project
app 1
app 2
app 3
Concepts
Apps are self-container: Each app is designed to hanlde a specific functionality, such as managing users, blogs, e-commerce features, or API endpoints
Reusable: they work across multiple Django projects. E.G.: You can create a "blog" app and reuse it in other projects with minimal modifications
Separation of concerns: Apps help organize the code logically. Instead of having all models, views and templates in a single place, each app contains its own model, views and templates
Example
E-commerce website project can be organized into apps, such as:
Users: Handles authentication, registration, and profile management
Products: Manages products listins, categories, and inventories
Orders: Deals with cart functionality, order creation and payments
ptyhon manage.py startapp app_name
Initial Project Folder Structure
project_name/ # root directory
├── manage.py # used for tasks such as running the server, creating apps, and applying migrations
├── project_name/
│ ├── __init__.py # it allows importing modules from thils folder
│ ├── asgi.py # async server gateway interface" entry point for async operations
│ ├── settings.py # contains all project-level config
│ ├── urls.py # maps urls to views
│ ├── wsgi.py # web server gateway interface: used to deploy dJANGO
└── db.sqlite3 (created after migrations)
Creating an app
Once our project is created, we can create an app with python manage.py startapp app_name
Once that is done, we need to register the app on the project settings.py. settings.py > INSTALLED_APP > add app_name
INSTALLED_APPS= [
...
'django.contrib.staticfiles',
"myapp"# we add the app(s) here
]
Initial App Folder Structure
app_name/
├── admin.py # used to register models with Django's admin interface
├── apps.py # contains config for the app
├── migrations/ # tracks database schema changes
│ └── __init__.py
├── models.py # defines the app's db schema using Django ORM
├── tests.py
├── views.py # handles logic for responding to HTTP requests
├── __init__.py # marks the directory as Python package. Allows importing modules from this app.
├── urls.py # we create this one