Opened 5 years ago
Last modified 5 years ago
#32147 closed Bug
timezone error when casting a naive date in a Daylight Saving Time Change — at Initial Version
| Reported by: | Clara | Owned by: | nobody |
|---|---|---|---|
| Component: | Utilities | Version: | 3.1 |
| Severity: | Normal | Keywords: | timezone |
| Cc: | Aymeric Augustin | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When you use a model with datetime with a timezone active and naive data as input, it raise the follow error:
File "<basepath>/lib/python3.7/site-packages/pytz/tzinfo.py", line 234, in localize
raise AmbiguousTimeError(dt)
pytz.exceptions.AmbiguousTimeError: 2020-10-25 02:00:01
The steps to reproduce are:
- Use a model with datetime: In this example, we are going to create a user model that extends AbstractUser, but you could use any model with a datetime field
class User(AbstractUser):
class Meta:
db_table = 'my_user'
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(auto_now=True)
last_password_change = models.DateTimeField(null=True)
- Create an object with a string naive date:
User.objects.create(
email = "test@amail.it",
username = "user_25",
created_at = "2020-10-25 02:00:01"
)
- The timezone settings are:
TIME_ZONE = 'Europe/Rome'
USE_TZ = True
Database configuration has not timezone set
Note:
See TracTickets
for help on using tickets.