SuperCow
SuperCow

Reputation: 1613

Django: Having problems saving value of datetime.date as null in database

My form looks like this:

enter image description here

If the 'In Progress' box is checked I want the 'End Date' to be null in the MySQL db when I save the form.

In my model I have this to allow a value of null to be saved (I think?):

end_date = models.DateField(default=datetime.date, blank=True, null=True)

I don't know if this is the best way to do it, but then in my views.py I have this line:

if request.POST.get('end_year') == '- Year -':
    end_date = datetime.date(None, None, None)

(I would also have similar checks for the Month and Day)

But when I try to submit my form I get 'an integer is required' exception.

As some additional information, when I manually try to change end_date to null in the db I get this error: Incorrect date value: 'null' for column 'end_date' at row 1

I'm not really sure what the best way to approach this is. Any ideas would be appreciated. Thank you.

Upvotes: 1

Views: 1170

Answers (2)

Piyush S. Wanare
Piyush S. Wanare

Reputation: 4943

I think you should check for following:

  1. end_date = models.DateTimeField(blank=True,null=True)

  2. while updating end_date field as NULL just query like end_date = None rather than doing end_date = datetime.date(None, None, None).

Upvotes: 1

Adilet Maratov
Adilet Maratov

Reputation: 1382

It seems to me that if you delete the default from your end_date field, you are going to be ok.

end_date = models.DateField(blank=True, null=True)

And now, if it is In progress on your form, then just do not provide any data to end_date field. I want to say that you should NOT do this.

end_date = datetime.date(None, None, None)

But since there is no mode default for this field, when it is not In progress, just provide initial data to it from the view or form.

Upvotes: 3

Related Questions