# Validating the data to be posted.

**URL:** <https://forum.djangoproject.com/t/validating-the-data-to-be-posted/4853>\
**Category:** Using Django\
**Created:** [October 19, 2020, 5:57am UTC](https://forum.djangoproject.com/t/validating-the-data-to-be-posted/4853 "2020-10-19T05:57:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AashuSapkota](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@AashuSapkota](https://forum.djangoproject.com/u/AashuSapkota)\
**Post date:** [October 19, 2020, 5:57am UTC](https://forum.djangoproject.com/t/validating-the-data-to-be-posted/4853/1 "2020-10-19T05:57:20Z")

</div>

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_

```auto
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_

```auto
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_

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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 19, 2020, 2:23pm UTC](https://forum.djangoproject.com/t/validating-the-data-to-be-posted/4853/2 "2020-10-19T14:23:10Z")

</div>

The different ways that you can perform validation within your form are all described in the [Form and field validation page](https://docs.djangoproject.com/en/3.1/ref/forms/validation/) 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.

---

<div class="post-metadata">

**Author:** ![AashuSapkota](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@AashuSapkota](https://forum.djangoproject.com/u/AashuSapkota)\
**Post date:** [October 20, 2020, 3:02am UTC](https://forum.djangoproject.com/t/validating-the-data-to-be-posted/4853/3 "2020-10-20T03:02:13Z")

</div>

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 20, 2020, 3:49am UTC](https://forum.djangoproject.com/t/validating-the-data-to-be-posted/4853/4 "2020-10-20T03:49:12Z")

</div>

> [@AashuSapkota](#):
>
> ` donated_dates = Donation.objects.get(Donation.date_of_donation).filter(donor=current_donor)`

That’s not a valid query. You might want to review the usage of the [get method](https://docs.djangoproject.com/en/3.1/topics/db/queries/#retrieving-a-single-object-with-get) with model instances and how they relate to [filters](https://docs.djangoproject.com/en/3.1/ref/models/querysets/#django.db.models.query.QuerySet.filter).
