This repository contains the Django project for the interview, where we answer several questions related to Django ORM and performance optimization techniques.
- question1/: Contains the implementation and explanation for the first interview question.
- question2/: Contains the implementation and explanation for the second interview question.
- question3/: Contains the implementation and explanation for the third interview question.
In Django ORM, both select_related
and prefetch_related
are used to optimize database queries by reducing the number of queries required to retrieve related objects. However, they operate in different ways and are suitable for different use cases:
select_related
: Performs a SQL JOIN and includes the fields of the related object in the SELECT statement. This method is suitable for one-to-one and many-to-one relationships.prefetch_related
: Performs a separate lookup for each relationship and does the joining in Python. This method is suitable for one-to-many and many-to-many relationships.
In this project, we have implemented views to demonstrate the performance differences between select_related
and prefetch_related
. The results are displayed on a comparison page.
-
Clone the Repository:
-
Open your terminal.
-
Run the following command to clone the repository to your local machine:
git clone https://github.com/LucaXYB/ScriptChain_interview.git
-
Navigate to the project directory:
cd ScriptChain_interview/django_project
-
-
Set Up the Virtual Environment:
-
Ensure you are in the project directory, then create a virtual environment:
python3 -m venv myvenv
-
Activate the virtual environment:
source myvenv/bin/activate # On Windows use: myvenv\Scripts\activate
-
-
Install Dependencies:
-
Install all required dependencies from the
requirements.txt
file:pip install -r requirements.txt
-
-
Run Migrations to Set Up the Database:
-
Apply migrations to set up the database schema:
python manage.py migrate
-
-
Populate the Database with Test Data:
-
Run the custom management command to populate the database with test data:
python manage.py populate_test_data
-
-
Start the Django Development Server:
-
Run the following command to start the server:
python manage.py runserver
-
The development server should now be running at
http://127.0.0.1:8000/
.
-
-
View the Results:
- Access the following pages to see the results of the
select_related
andprefetch_related
queries:select_related
View: http://127.0.0.1:8000/question1/orders/prefetch_related
View: http://127.0.0.1:8000/question1/users/- Comparison Page: http://127.0.0.1:8000/question1/compare/
- Access the following pages to see the results of the
-
Question 1 Code:
- The core logic for
question1
is located in thequestion1/views.py
file. - The comparison results and details are displayed in
question1/templates/question1/compare_methods.html
.
- The core logic for
-
Models:
- The models for the
question1
app are located inquestion1/models.py
.
- The models for the
Q
objects in Django ORM are used to encapsulate a collection of keyword arguments, allowing for complex queries with multiple conditions. They enable logical operators like AND
, OR
, and NOT
within queries and can combine multiple conditions to create dynamic queries.
In this project, we have implemented various types of queries using Q
objects, including OR
queries, AND
queries, NOT
queries, combining multiple Q
objects, and nested queries. The results of these queries are displayed on a web page, alongside a table that explains the definition, advantages, and disadvantages of Q
objects.
-
Clone the Repository:
- If you haven't already cloned the repository, follow the steps outlined in Question 1.
-
Set Up the Virtual Environment and Install Dependencies:
- Ensure you have activated the virtual environment and installed the dependencies as mentioned in Question 1.
-
Run Migrations to Set Up the Database:
-
Apply migrations to set up the database schema for
question2
:python manage.py makemigrations question2 python manage.py migrate
-
-
Start the Django Development Server:
-
Start the server:
python manage.py runserver
-
-
View the Results:
- Access the following page to see the results of the
Q
object queries:- Q Object Queries View: http://127.0.0.1:8000/question2/q-object-queries/
- Access the following page to see the results of the
-
Question 2 Code:
- The core logic for
question2
is located in thequestion2/views.py
file. - The various
Q
object query results and details are displayed inquestion2/templates/question2/q_object_queries.html
.
- The core logic for
-
Models:
- The models for the
question2
app are located inquestion2/models.py
.
- The models for the
In this task, you will deploy a Django application on an AWS EC2 instance, utilizing the free tier to minimize costs. The deployment involves several steps, including setting up the EC2 instance, installing necessary dependencies, configuring Gunicorn as the application server, and using Nginx as the reverse proxy. This setup ensures that your Django application can handle incoming requests efficiently and securely.
-
Sign in to AWS Management Console:
- Go to the AWS Management Console and log in with your credentials.
-
Navigate to EC2 Dashboard:
- In the AWS Management Console, search for "EC2" in the services search bar and select "EC2" to open the EC2 Dashboard.
-
Launch a New Instance:
- Click on the "Launch Instance" button on the EC2 Dashboard to start creating a new EC2 instance.
-
Choose Amazon Machine Image (AMI):
- Under "Choose an Amazon Machine Image (AMI)", select the "Amazon Linux 2 AMI (HVM), SSD Volume Type" (which is free tier eligible).
-
Select Instance Type:
- In the "Choose an Instance Type" section, select the
t2.micro
instance type, which is also free tier eligible. Click "Next: Configure Instance Details".
- In the "Choose an Instance Type" section, select the
-
Configure Instance Details:
- Leave the default settings and click "Next: Add Storage".
-
Add Storage:
- The default storage configuration is typically sufficient. Click "Next: Add Tags".
-
Add Tags:
- You can optionally add tags to help identify your instance. For example, you can add a tag with
Key
asName
andValue
asMyDjangoAppInstance
. Click "Next: Configure Security Group".
- You can optionally add tags to help identify your instance. For example, you can add a tag with
-
Configure Security Group:
- SSH: By default, SSH (port 22) will be enabled. This allows you to connect to your instance remotely.
- HTTP: Add a new rule to allow HTTP (port 80) traffic, which is necessary to serve your Django application.
- HTTPS (optional): If you plan to use HTTPS, you should also allow HTTPS (port 443).
- Ensure the source is set to
0.0.0.0/0
to allow access from anywhere, or limit it to your IP address for added security. - Click "Review and Launch".
-
Review and Launch:
- Review your settings, then click "Launch".
- When prompted, create a new key pair or select an existing one. This key pair will be used to connect to your instance via SSH.
- Download the
.pem
file and store it securely. You will need this to connect to your instance.
-
Launch Instance:
- Click "Launch Instances". Your EC2 instance will start launching.
-
View Instance:
- Click "View Instances" to go back to the EC2 Dashboard, where you can see your new instance starting up. Wait until the instance's "Instance State" changes to "running".
-
Move the
.pem
File to the.ssh
Directory:- Move the
.pem
file you downloaded to your local machine's.ssh
directory for better security practices:
mv ~/Downloads/your-key-pair.pem ~/.ssh/
- Move the
-
Set the Permissions for the Key Pair:
- Set the correct permissions for the .pem file to make it secure
chmod 400 ~/.ssh/your-key-pair.pem
-
Connect to the EC2 Instance:
- Use the following SSH command to connect to your EC2 instance, replacing your-key-pair.pem with your key pair's name and ec2-user@your-ec2-public-ip with the public IP address of your instance:
ssh -i ~/.ssh/your-key-pair.pem ec2-user@your-ec2-public-ip
-
Verify the Connection:
- Once connected, you should see a terminal prompt that indicates you're logged into the EC2 instance. You can now run commands on your remote instance.
-
Update the package list:
- After connecting to your EC2 instance, run the following command to update the package list:
sudo yum update -y
-
Install Git, Python, and pip:
- Install Git to clone your repository and install Python 3 and pip (Python's package installer):
sudo yum install git -y sudo yum install python3 -y sudo yum install python3-pip -y
-
Install virtualenv:
- Install virtualenv to create an isolated environment for your project:
sudo pip3 install virtualenv
-
Navigate to the home directory:
- Ensure you're in the home directory of the EC2 instance:
cd ~
-
Clone the repository:
- Clone your GitHub repository to the EC2 instance using the following command:
git clone https://github.com/LucaXYB/ScriptChain_interview.git
-
Navigate to the project directory:
- After cloning, navigate to the Django project directory:
cd ScriptChain_interview/django_project
-
Create a virtual environment:
- Create a virtual environment within the project directory:
python3 -m venv myvenv
-
Activate the virtual environment:
- Activate the virtual environment:
source myvenv/bin/activate
-
Install project dependencies:
- Install the necessary dependencies from the requirements.txt file:
pip install -r requirements.txt
-
Create and edit the Gunicorn service file:
- Create a new systemd service file for Gunicorn:
sudo nano /etc/systemd/system/gunicorn.service
-
Add the following content to the service file:
- Ensure the paths are correct according to your project structure:
[Unit] Description=gunicorn daemon for Django Project After=network.target [Service] User=ec2-user Group=nginx WorkingDirectory=/home/ec2-user/ScriptChain_interview/django_project ExecStart=/home/ec2-user/ScriptChain_interview/django_project/myvenv/bin/gunicorn --workers 3 --bind unix:/home/ec2-user/ScriptChain_interview/django_project/gunicorn.sock django_project.wsgi:application [Install] WantedBy=multi-user.target
-
Save and exit:
- Save the file (Ctrl + O), then exit (Ctrl + X).
-
Start and enable Gunicorn:
- Reload systemd to apply the changes, then start and enable Gunicorn:
sudo systemctl daemon-reload sudo systemctl start gunicorn sudo systemctl enable gunicorn
-
Check Gunicorn status:
- Ensure Gunicorn is running:
sudo systemctl status gunicorn
- Confirm that the service is active and running.
-
Install Nginx:
- Install Nginx if it’s not already installed:
sudo yum install nginx -y
-
Create and edit the Nginx configuration file:
- Create a new Nginx configuration file for your Django project:
sudo nano /etc/nginx/conf.d/django_project.conf
-
Add the following content to the configuration file:
- Ensure the proxy_pass path matches the Gunicorn socket file location:
server { listen 80; server_name your-ec2-public-ip; location / { proxy_pass http://unix:/home/ec2-user/ScriptChain_interview/django_project/gunicorn.sock; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location /static/ { alias /home/ec2-user/ScriptChain_interview/django_project/static/; } location /media/ { alias /home/ec2-user/ScriptChain_interview/django_project/media/; } }
-
Save and exit:
- Save the file (Ctrl + O), then exit (Ctrl + X).
-
Test the Nginx configuration:
- Ensure there are no syntax errors in the configuration:
sudo nginx -t
-
Start and enable Nginx:
- Start Nginx and enable it to start on boot:
sudo systemctl start nginx sudo systemctl enable nginx
-
Reload Nginx:
- Reload Nginx to apply the new configuration:
sudo systemctl reload nginx
- Open your browser:
- Visit your EC2 instance's public IP address in your browser:
http://your-ec2-public-ip/
- Verify the Django application:
- You should see your Django application's homepage or whichever page you set as the default. This indicates that your project has been successfully deployed.