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

Updating Multiple Records

In Django, the update() method is used to update one or more records in the database that match a specific set of criteria. Unlike the save() method, which updates a single instance, the update() method operates on a QuerySet and allows you to perform bulk updates efficiently.

Key Points:

  1. Bulk Updates: The primary purpose of the update() method is to perform bulk updates on multiple records simultaneously, which is more efficient than updating each record individually using the save() method.
  2. No Signal Handling: Unlike the save() method, the update() method does not trigger any model signals (e.g., pre_save or post_save signals). This can be an important consideration in some scenarios.
  3. Field Lookups: You can use field lookups in the update() method to filter the records you want to update. This allows you to target specific records for modification.

For example, lets say we add another filed in our model, is_active. It would be a boolean field to demonstrate how multiple values can be updated at once. So lets start by making change in our model.

is_active = models.BooleanField(default=True)

After adding this field in the model, you need to run python manage.py makemigrations and python manage.py migrate in order to make changes in the database.

python manage.py makemigrations
python manage.py migrate

alt text Now the column has been added to the database. If you retrieve the values from the database now, you would also get the is_active column.

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

alt textAs you can see the is_active column is present and its default value is True.

Now lets update the is_active value, for all Persons who were born before 1960 to False. You can use the update() method as follows :

Person.objects.filter(birth_date__year__lt=1960).update(is_active=False)

alt textIt will return the number of rows affected, after the update has been performed.

You can print and check the rows that have been affected :

Person.objects.filter(birth_date__year__lt=1960).values()

alt textAs you can see the is_active column for these rows has been set to False, like this the update() method, can be used to update multiple rows at once.