How can I validate data to be posted on a table with reference to the same table?

While creating donation I want to check whether the data with same donor_id exists or not. If data don't exists then create donation else if there is data then get the latest donated date and validate whether the latest donated is greater then 3 months or not (if latest donated date exceeds three month then create donation else display message with donation cannot be made.)

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__'



Read more here: https://stackoverflow.com/questions/64398537/how-can-i-validate-data-to-be-posted-on-a-table-with-reference-to-the-same-table

Content Attribution

This content was originally published by Sabin Sapkota at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: