Django

College Event Registration Website

CampusConnect Introduction and Setup Configuring settings file for template, static and media files Setting up Tailwind Creating Custom User Model Creating Super User for CampusConnect Registering Custom User Model Search and Filter for User Model Serving Media Files During Development Categorizing Departments Linking Department with HOD Creating Abstract Model for Event Creating Model for Workshop Customizing Admin Page for Workshop Update in Model AbstractEvent Adding Instructor for Workshop Instructor Model Admin Page Adding Poster Field in Abstract Event Providing Access to HOD Access Update for HOD Restricting HOD Access to Particular Department AbstractEvent On Spot Registration Field Creating Workshop Object Creating and Linking Home Page Displaying Workshop on Home Page Styling Home Page Adding Workshop Detail Page Link Workshop Detail Page Workshop Detail Page Styling Workshop Instructor Details Workshop Detail Contact Contact Admin Page Many to Many Field for Contact Displaying Contact on Workshop Detail Page Adding Title for Workshop Detail Page Adding Gallery for Workshop Workshop Gallery Admin Page Displaying Gallery Images on Website Through Context Displaying Gallery Images on Website through template tags Authentication for users User Registration User Registration Submission Logout Functionality For User Login Functionality for User Model For Workshop Registration Workshop Registration Admin Page Register Workshop Function Register Button in Workshop Page Validations Before Workshop Registration Workshop Registration Closed Validaiton User Already Registered for Workshop Validation Workshop Registration Report From Admin Page Export using Library in Django Admin Extending Abstract Event for Hackathons

F expressions

F expressions in Django allow you to perform database operations and calculations directly in the database engine rather than retrieving data and processing it in Python code. They provide a way to reference database fields and perform operations on those fields within queries. F expressions are particularly useful for optimizing database queries and reducing the amount of data transferred between the database and your application.

Key Points:

  1. Performing Operations in the Database: F expressions enable you to perform database-level operations, such as addition, subtraction, multiplication, and division, directly within your queries.
  2. Efficient Queries: By using F expressions, you can avoid retrieving records from the database only to perform calculations in Python, which can be slow and resource-intensive. Instead, the calculations are performed in the database engine, leading to more efficient queries.
  3. Usage in Queries: You can use F expressions in various parts of queries, including filters, updates, and annotations.

Example, say you add a field, full_name in the model and want to update all the existing values in the database. It can be done as follows, first open the models.py file and add the field full_name in the Person model.

full_name = models.CharField(max_length=60, null=True)

Add null=True, so that it doesn't show any error while adding the field. After updating the models.py file, you can run the python manage.py makemigrations and python manage.py migrate commands to make the changes in the database.

python manage.py makemigrations
python manage.py migrate

alt text After the database is migrated, you can open the shell and type the command,

vars(Person.objects.get(id=1))

alt textCurrently, the full_name field value is None for all the fields. You can update the full_name field value for all the fields as follows :

from django.db.models import F, Value
from django.db.models.functions import Concat
Person.objects.update(full_name=Concat(F('first_name'), Value(' '), F('last_name')))

alt textThe numbers of rows updated has been returned as the output.

Now, if you try to view the values, the full_name will be the concatenation of first_name and last_name :

vars(Person.objects.get(id=1))

alt text F expressions can also be used in filter conditions, suppose you want to get the details of the Persons whose birth day and month are equal, you can use a query as follows :

Person.objects.filter(birth_date__day=F('birth_date__month')).values()

alt text F expressions provide a powerful way to work with database fields and perform calculations efficiently in Django. They are particularly valuable for optimizing database-related operations in your web applications.