﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34828	DateTimeField breaks when given datetime that would be invalid in UTC	Denis Cornehl	nobody	"We had a strange production error which I think is due to a Django bug, but please correct me if I'm wrong :) 

We have `USE_TZ` active, our own timezone set (Europe/Berlin), and a user entered `0001-01-01 00:00:00` into a datetimefield in a form. 

This value is now put into the postgres `timestamp with tz` field including its timezone (` 0001-01-01 00:00:28+00:53:28` for example). 

When I now try to read the model instance from the database, I'm getting a `ValueError: year -1 is out of range ` exception: 

{{{
  File ""[...]/lib/python3.11/site-packages/django/db/models/manager.py"", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""[...]/lib/python3.11/site-packages/django/db/models/query.py"", line 633, in get
    num = len(clone)
          ^^^^^^^^^^
  File ""[...]/lib/python3.11/site-packages/django/db/models/query.py"", line 380, in __len__
    self._fetch_all()
  File ""[...]/lib/python3.11/site-packages/django/db/models/query.py"", line 1881, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""[...]/lib/python3.11/site-packages/django/db/models/query.py"", line 91, in __iter__
    results = compiler.execute_sql(
              ^^^^^^^^^^^^^^^^^^^^^
  File ""[...]/lib/python3.11/site-packages/django/db/models/sql/compiler.py"", line 1595, in execute_sql
    return list(result)
           ^^^^^^^^^^^^
  File ""[...]/lib/python3.11/site-packages/django/db/models/sql/compiler.py"", line 2093, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File ""[...]/lib/python3.11/site-packages/django/db/models/sql/compiler.py"", line 2093, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""[...]/lib/python3.11/site-packages/django/db/utils.py"", line 98, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
ValueError: year -1 is out of range

}}}

I'm aware that the postgres timestamp supports a wider range than the python datetime, but then I would have assumed that the field wouldn't even accept values it can't handle. 
In this specific case I see that we have a timezone aware datetime in python, store it into the database with timezone, and `psycopg2` fetches it as timezone aware datetime. So I'm not sure why it has to be valid in UTC too. 


This can be reproduced using a simple model: 
{{{#!python
class TestModel(models.Model):
    d = models.DateTimeField()
}}}

And the following code: 
{{{#!python
from brokendate.models import TestModel
from datetime import datetime, date
from pytz import timezone

TestModel.objects.all().delete()
ok = datetime(1,1,1, 0,0,0, tzinfo=timezone(""Europe/Berlin""))
t = TestModel()
# assign datetime that would be invalid in UTC
t.d = ok
t.save()

print(""fetch object"")
# this raises the exception
t = TestModel.objects.get()
print(t.d)
}}}

I've created a small django project / app with a `break` management command that will reproduce this problem. 


"	Bug	closed	Database layer (models, ORM)	4.2	Normal	wontfix			Unreviewed	0	0	0	0	0	0
