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

Unique Parameter

In Django models, the unique parameter is a boolean attribute that can be added to fields to enforce uniqueness constraint on the values stored in that field. When a field is marked as unique=True, it ensures that no two records in the database can have the same value in that field. If a duplicate value is attempted to be saved, Django will raise a django.db.IntegrityError during database operations, preventing the duplicate entry.

Lets add 2 fields inside the person class for a demo :

email = models.EmailField(unique=True, null=True)
username = models.CharField(max_length=30,unique=True, null=True)

You need to run python manage.py makemigrations and python manage.py migrate to add the fields inside the model.

python manage.py makemigrations
python manage.py migrate

alt text Now the fields are added and you can assign value for them.

from modeldemo.models import Person
p=Person.objects.get(id=1)
vars(p)
p.email="test@edkool.com"
p.username="edkool"
p.save()
vars(p)

alt textNow, if you try to assign the same values to some other row, it will show an error.

p=Person.objects.create(first_name='Test',last_name='Demo', birth_date='1996-01-01', username='edkool')

The error would be like :

sqlite3.IntegrityError: UNIQUE constraint failed: modeldemo_person.username

alt text The same error would occur, even if the email field gets a duplicate value.

p=Person.objects.create(first_name='Test',last_name='Demo', birth_date='1996-01-01', email='test@edkool.com')

The error would look like :

sqlite3.IntegrityError: UNIQUE constraint failed: modeldemo_person.email

alt text