Validating the data to be posted.

I am beginner to Django. I am creating the program to keep record of blood donor.
While creating donation I want to check whether the person already donated or not. If the person have not donated blood previously then I want to save the data directly and if the person have donated the blood previously I want to check whether the time difference between previously donated date and current date exceeds three months or not. If the time difference exceeds then only I want to save the data else do not allow the donor to donate the blood.
Currently I have not added any custom template I have been using modelform. I have no idea how I can validate the data Below are my codes of models and views file.
Code of models.py

from django.db import models

GENDER_CHOICES = (
    ('M', 'MALE'),
    ('F', 'FEMALE'),
    ('O', 'Others'),
)
BLOOD_GROUP_CHOICES = (
    ('A+', 'A+'),
    ('A-', 'A-'),
    ('B+', 'B+'),
    ('B-', 'B-'),
    ('O+', 'O+'),
    ('O-', 'O-'),
    ('AB+', 'AB+'),
    ('AB-', 'AB-'),
)


class Donor(models.Model):
    first_name = models.CharField(max_length=20)
    middle_name = models.CharField(max_length=20, blank=True)
    last_name = models.CharField(max_length=20)
    blood_group = models.CharField(choices=BLOOD_GROUP_CHOICES, max_length=3, null=True)
    gender = models.CharField(choices=GENDER_CHOICES, max_length=1)
    email = models.EmailField(blank=True)
    mobile_number = models.CharField(max_length=15)
    telephone_number = models.CharField(blank=True, max_length=15)
    date_of_birth = models.DateField()

    def __str__(self):
        return self.first_name + ' ' + self.last_name


class Donation(models.Model):
    donor = models.ForeignKey(Donor, related_name='donor_name', on_delete=models.CASCADE)
    date_of_donation = models.DateField()
    donation_address = models.CharField(max_length=200)

    def __str__(self):
        return self.donor.first_name

code of views.py

from django.shortcuts import render, redirect
from .forms import DonorForm, DonationForm
from .models import Donor, Donation
from datetime import datetime


def donorPage(request):
    donors = Donor.objects.all()

    context = {'donor': donors}

    return render(request, 'donor/donor_page.html', context)


def createDonor(request):
    form = DonorForm()

    if request.method == 'POST':
        form = DonorForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('donor_page')
    context = {'form': form}
    return render(request, 'donor/donor_form.html', context)


def updateDonor(request, pk):
    donor = Donor.objects.get(id=pk)
    form = DonorForm(instance=donor)

    if request.method == 'POST':
        form = DonorForm(request.POST, instance=donor)
        if form.is_valid():
            form.save()
            return redirect('donor_page')

    context = {'form': form}

    return render(request, 'donor/donor_form.html', context)


def deleteDonor(request, pk):
    donor = Donor.objects.get(id=pk)

    if request.method == "POST":
        donor.delete()
        return redirect('donor_page')
    context = {'item': donor}
    return render(request, 'donor/donor_delete.html', context)


def donationPage(request):
    donations = Donation.objects.all()

    context = {'donation': donations}

    return render(request, 'donation/donation_page.html', context)


def createDonation(request):
    form = DonationForm()

    if request.method == 'POST':
        form = DonationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('donation_page')
    context = {'form': form}
    return render(request, 'donation/donation_form.html', context)


def updateDonation(request, pk):
    donation = Donation.objects.get(id=pk)
    form = DonationForm(instance=donation)

    if request.method == 'POST':
        form = DonationForm(request.POST, instance=donation)
        if form.is_valid():
            form.save()
            return redirect('donation_page')

    context = {'form': form}

    return render(request, 'donation/donation_form.html', context)


def deleteDonation(request, pk):
    donation = Donation.objects.get(id=pk)

    if request.method == "POST":
        donation.delete()
        return redirect('donation_page')
    context = {'item': donation}
    return render(request, 'donation/donation_delete.html', context)

Code of forms.py

from django.forms import ModelForm
from .models import Donor, Donation


class DonorForm(ModelForm):
    class Meta:
        model = Donor
        fields = '__all__'


class DonationForm(ModelForm):
    class Meta:
        model = Donation
        fields = '__all__'

The different ways that you can perform validation within your form are all described in the Form and field validation page in the docs.

You didn’t supply your form, so I can’t say exactly what functions you need, but since you’re going to be comparing the entered data to other models, I’m thinking you would likely want to implement a clean method on the entire form.

As of your suggestion I have tried using clean method. Using the Clean method I am able to get current donor and current date_of_donation. But when I tried to access the data from database with same donor then I got error “cannot unpack non-iterable DeferredAttribute object”.
Below is my code of forms.py

from django.forms import ModelForm
from .models import Donor, Donation

class DonorForm(ModelForm):
    class Meta:
        model = Donor
        fields = '__all__'


class DonationForm(ModelForm):
    class Meta:
        model = Donation
        fields = '__all__'

    def clean(self):
        cleaned_data = super().clean()
        current_donor = cleaned_data.get("donor")
        current_donated_date = cleaned_data.get("date_of_donation")
        donated_dates = Donation.objects.get(Donation.date_of_donation).filter(donor=current_donor)
        print(current_donor)
        print(current_donated_date)
        print(donated_dates)

That’s not a valid query. You might want to review the usage of the get method with model instances and how they relate to filters.