0% found this document useful (0 votes)
41 views4 pages

Commands

Uploaded by

Harshit Pathak
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)
41 views4 pages

Commands

Uploaded by

Harshit Pathak
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/ 4

create env --> python3 -m venv env

activate env --> source env/bin/activate or workon <env name>


create project --> django-admin startproject <projectname>
move to the project directory --> cd testproject
run --> python3 manage.py runserver
create app --> python manage.py startapp <app_name>

to connect to postgress --> go to setting of project i.e testproject

need a connector to connect python with postgress --> use data base adapter--
>psycopg2

now, for tables to be created a model is needed

we have a models file that contains simple class,


so need to inherit model in the class
eg. class Destination(models.Model)

still it will not work bcoz we need table type too

search for django model fields in their documentation

before migrating check if app is written in setting.py INSTALLED_APPS of project or


not

now make a migration --> python

created superuser for admin page of postgress

to get yours table in the admin page --> register your model in admin.py

Certainly! Django provides a powerful ORM (Object-Relational Mapping) that allows


you to perform advanced queries on PostgreSQL tables. Here are some examples of
advanced queries you can perform using Django ORM:

Filtering:

Retrieve all students with the name "John":

Student.objects.filter(name="John")
Retrieve all students in a specific department:

Student.objects.filter(department__name="Computer Science")
Joins:

Retrieve all students along with their department information:

Student.objects.select_related('department')
Aggregation:

Count the number of students:


Student.objects.count()
Get the average age of students:

from django.db.models import Avg


Student.objects.aggregate(avg_age=Avg('age'))
Annotations:

Add an annotation to each student with the count of departments they belong to:

from django.db.models import Count


Student.objects.annotate(num_departments=Count('department'))
Distinct:

Retrieve unique department names:

Department.objects.values_list('name', flat=True).distinct()
Raw SQL Queries:

Execute custom SQL queries:

from django.db import connection


with connection.cursor() as cursor:
cursor.execute("SELECT * FROM app1_student WHERE age > %s", [20])
students = cursor.fetchall()

Subqueries:

Use a subquery to filter students based on the average age of students in a


department:
python

from django.db.models import Subquery, OuterRef


subquery = Department.objects.filter(name="Computer
Science").annotate(avg_age=Avg('student__age')).values('avg_age')
students = Student.objects.filter(age__gt=Subquery(subquery))

# 6 March, 24

--> Resolved Bugs


--> Populated Dummy data to E-commerce model
--> Studied about User Model for auth and post apis
--> Created Custom Superuser and solved bugs
--> Populated Dummy data again
--> writen few more apis
Bug
Unable to get custom user in the AUTH section of admin page

#7 March
Cutsom User Models all bugs solved
Created custom user model with userid as additional field that acts as PK
for superuser and other users too (earlier i was only able to do for superuser)
for authentication purpose

#8 March
token based authentication
Login/Logout apis created
set few apis as 'login required'

Bugs: able to login and get csrf Token but unable to Logout..token mismatch

Notes:
Token Generation
--When a user logs in or starts a session, Django generates a random and unique
CSRF token for that session.
--This token is usually a long string of characters. This token is associated with
the user’s session and stored on the server.
--CsrfViewMiddleware sends this cookie with the response whenever
django.middleware.csrf.get_token() is called.
--It can also send it in other cases. For security reasons, the value of the secret
is changed each time a user logs in.

#11 March

studied csrf token and its use


studied session authorization
worked upon bugs and resolved them related to token auth
Added permission denied to apis for logged off users

#12 March
Token Auth

#13 March
JWT Auth

Access Token:
Purpose: An access token is a credential used to access protected resources on
behalf of the authenticated user.
It carries the necessary information to access a resource directly.
Lifetime: Access tokens typically have a short lifespan, usually ranging from a few
minutes to a few hours.
Usage: When a user logs in or authenticates, they are issued an access token.
This token is then sent along with each request to the server to access
protected resources.
The server verifies the access token to ensure that the user is authorized
to access the requested resource.

Refresh Token:
Purpose: A refresh token is a long-lived credential used to obtain a new access
token after the current access token expires.
It allows the user to maintain their session without needing to log in
again.
Lifetime: Refresh tokens have a longer lifespan compared to access tokens, often
ranging from days to weeks.
Usage: After the access token expires, the client application can use the refresh
token to request a new access token
from the authentication server without requiring the user to re-enter their
credentials.
This helps improve security by reducing the exposure of the user's
credentials.
Refresh tokens are typically exchanged for a new access token through a
specific endpoint provided by the authentication server.

# 2nd April

Homebrew Installation
Redis setup
Celery Setup
Basic code with Celery

#3rd April

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy