-2

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?

the wind
  • 235
  • 2
  • 12

1 Answers1

2

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.

Alain Bianchini
  • 3,883
  • 1
  • 7
  • 27