pythondjangodatemulti-value-dictionary

MultiValueDictKeyError at /update 'date'


I'm trying to update my database but I encountered MultiValueDictKeyError.

The problem is triggered by 'date' as:

t.date = request.POST.get('date')

I tried

print(request.POST)

with the result:

<QueryDict: {'csrfmiddlewaretoken': ['9mgfDaQRsH4Pv5rvglufS3wC61QDL5i9tcOqmBQwNFAKFpzE79h9wBY8St9CwBsB'], 'ID_number': ['4'], 'date_month': ['1'], 'date_day': ['1'], 'date_year': ['2020'], 'first_name': ['Gedo'], 'last_name': ['Prasad'], 'Membership_Start_date_month': ['1'], 'Membership_Start_date_day': ['1'], 'Membership_Start_date_year': ['2020'], 'Membership_End_date_month': ['4'], 'Membership_End_date_day': ['1'], 'Membership_End_date_year': ['2020'], 'member_type': ['Gym&Sauna'], 'payment_type': ['3 Month'], 'rate': ['23'], 'paid': ['20'], 'due': ['3'], 'Contact_number': ['1121212129'], 'Email': ['gedo@pzrasad.com'], 'Remarks': ['gsa']}>

Notice that the date field gives 'date_month', 'date_day', 'date_year' separately. Maybe that's the problem. But I don't know the answer. Any suggestions would be very helpful as I am a student and a beginner.

Here is my models.py:

class Member(models.Model):
    ID_number = models.IntegerField()
    date = models.DateField(verbose_name='date')
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    member_type = models.CharField(
        max_length=30,
        choices=MEMBERSHIP_CHOICES,
    )
    payment_type = models.CharField(
        max_length=30,
        choices=PAYMENT_TYPE_CHOICES,
    )
    Membership_Start_date = models.DateField(verbose_name='Membership_Start_date')
    Membership_End_date = models.DateField(verbose_name='Membership_End_date')
    rate = models.IntegerField()
    paid = models.IntegerField()
    due = models.IntegerField()
    Contact_number = models.CharField(max_length=14)
    Email = models.EmailField()
    Remarks = models.CharField(max_length=255, blank = True, null = True)

def __str__(self):
    return str(self.ID_number) + "  " + self.first_name + " " + self.last_name

Solution

  • So, I did find a workaround based on the format the date field takes i.e. YYYY-MM-DD:

    t.date = request.POST['date_year'] + "-" + request.POST['date_month'] + "-" + request.POST['date_day']
    

    But is there any other way?