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

Char Field

In Django, the CharField is a field type used to store short to medium-length strings, such as names, titles, and other text data. It's one of the most commonly used field types for text-based data in Django models.

Parameters and Attributes:

  1. max_length (required): This parameter specifies the maximum number of characters allowed in the field. It is a required parameter for CharField. For example, max_length=50 limits the field to 50 characters.
  2. blank: A boolean parameter that, when set to True, allows the field to be left blank (i.e., it's not required). The default is False. It is useful while creating Forms.
  3. null: A boolean parameter that, when set to True, allows the field to have a NULL database value. The default is False.
  4. default: This parameter allows you to set a default value for the field. For example, default='Default Value' sets the default value to 'Default Value'.
  5. choices: You can specify a list of choices using the choices parameter, which restricts the allowed values to a predefined set.
  6. unique: You can also specify a column to take only unique values. If a field is set to unique, then it will show an error when the duplicate values are tried to be inserted in that field. The default is False. More details about the unique parameter can be viewed here.
  7. validators: You can add custom validation functions using the validators parameter to ensure that the data in the field meets specific criteria.

A model example can be as follows :

class Person(models.Model):
    GENDER_CHOICES = (
    ('M', 'Male'),
    ('F', 'Female'),
    )
    first_name = models.CharField(max_length=30)
    bio = models.CharField(max_length=500, blank=True, null=True)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default="M")

Here,

  1. first_name is a Char Field with a maximum lenght of 30.
  2. bio is given a Char Field with blank=True and null=True, which means if the field is not given while creating an object it won't cause any error.
  3. The gender has a set of choices that allows only two values in it, and if it is not provided it will take the default value as M.

You need to run the python manage.py makemigrations and python manage.py migrate commands to create the model.

python manage.py makemigrations
python manage.py migrate

alt textYou can create objects for the model as follows :

from datatypedemo.models import Person
p=Person.objects.create(first_name='EDKOOL')
vars(p)

alt textHere, since no value has been given for the gender its taken as M by default. Also the bio is None as it has not been specified.

You can add bio and change the value of gender as well for the object.

p.bio="This is Edkool's bio"
p.gender='F'
p.save()

alt textAs the values of bio and gender has changed there are updated correspondingly.

Furthermore, you can add validations for particular fields, say you want to add a validation that if the bio field is present, it should containt at least 50 characters, you can add it as follows :

def validate_bio(value):
    if value and len(value)<50:
        raise ValidationError(f"Length of Bio is {len(value)}. It should be atleast 50 characters")

class Person(models.Model):
    GENDER_CHOICES = (
    ('M', 'Male'),
    ('F', 'Female'),
    )
    first_name = models.CharField(max_length=30)
    bio = models.CharField(max_length=500, validators=[validate_bio],blank=True, null=True)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES,default="M")

You need to run python manage.py makemigrations and python manage.py migrate in order to incorporate the changes.

python manage.py makemigrations
python manage.py migrate

alt textNow, if you check the object that was created, it will show a validation error.

from datatypedemo.models import Person
p=Person.objects.get(id=1)
p.full_clean()

alt textThe error is as follows :

raise ValidationError(errors)
django.core.exceptions.ValidationError: {'bio': ['Length of Bio is 20. It should be atleast 50 characters']}

If you try to assign the bio to None or put more than 50 characters then the validation error will go away.

p.bio=None
p.save()
p.full_clean()

alt textAs you can see, no error has been displayed as the bio is set to None.

The CharField in Django is highly versatile and customizable, making it a suitable choice for storing various text-based data in your models. It's important to choose an appropriate max_length value based on your data requirements to ensure data integrity in the database.