'error : invalid literal for int() with base 10: b'20 Feb'

I am working with Wagtail and I am creating the model of the app.

publish_date = models.DateField(
        max_length=300,
        blank=True,
        null=True,
        default="20 Feb",
        verbose_name="First publish date",
        help_text="This shows the first publish date"
    )

I think the problem is that the field type is a DateField but the value that I am sending is '20 Feb'.

Any ideas?



Solution 1:[1]

You can' t use a string, DateField requires a datetime.date object. If you always want the same date, you can write:

import datetime
publish_date = models.DateField(
        max_length=300,
        blank=True,
        null=True,
        default=datetime.date(2022, 2, 20),  # Or another date
        verbose_name="First publish date",
        help_text="This shows the first publish date"
    )

If you want the current date as the default:

import datetime
publish_date = models.DateField(
        max_length=300,
        blank=True,
        null=True,
        default=datetime.date.today,
        verbose_name="First publish date",
        help_text="This shows the first publish date"
    )

If you support timezones you should use django.utils.timezone instead of datetime.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RedWheelbarrow