Code

Ticket #3408: __init__.py.diff

File __init__.py.diff, 2.7 KB (added by Brantley <deadwisdom@…>, 7 years ago)

/django/db/models/fields/init.py

Line 
1Index: __init__.py
2===================================================================
3--- __init__.py (revision 4454)
4+++ __init__.py (working copy)
5@@ -437,6 +437,11 @@
6         Field.__init__(self, verbose_name, name, **kwargs)
7 
8     def to_python(self, value):
9+        if value is None:
10+            if self.null:
11+                return value
12+            else:
13+                raise validators.ValidationError, gettext_lazy("This field cannot be null.")
14         if isinstance(value, datetime.datetime):
15             return value.date()
16         if isinstance(value, datetime.date):
17@@ -478,10 +483,13 @@
18             return override
19         else:
20             return self.editable or self.auto_now or self.auto_now_add
21-
22+           
23     def get_db_prep_save(self, value):
24         # Casts dates into string format for entry into database.
25-        if value is not None:
26+        # MINE: if value is not None and type(value) != type(''):
27+        if isinstance(value, datetime.datetime):
28+            value = value.date().strftime('%Y-%m-%d')
29+        elif isinstance(value, datetime.date):
30             value = value.strftime('%Y-%m-%d')
31         return Field.get_db_prep_save(self, value)
32 
33@@ -499,6 +507,11 @@
34 
35 class DateTimeField(DateField):
36     def to_python(self, value):
37+        if value is None:
38+            if self.null:
39+                return value
40+            else:
41+                raise validators.ValidationError, gettext_lazy("This field cannot be null.")
42         if isinstance(value, datetime.datetime):
43             return value
44         if isinstance(value, datetime.date):
45@@ -782,7 +795,25 @@
46         if auto_now or auto_now_add:
47             kwargs['editable'] = False
48         Field.__init__(self, verbose_name, name, **kwargs)
49-
50+       
51+    def to_python(self, value):
52+        if value is None:
53+            if self.null:
54+                return value
55+            else:
56+                raise validators.ValidationError, gettext_lazy("This field cannot be null.")
57+        if isinstance(value, datetime.time):
58+            return value
59+        if isinstance(value, datetime.datetime):
60+            return value.time()
61+        try: # Seconds are optional, so try converting seconds first.
62+            return datetime.time(*time.strptime(value, '%H:%M:%S')[3:6])
63+        except ValueError:
64+            try: # Try without seconds.
65+                return datetime.time(*time.strptime(value, '%H:%M')[3:5])
66+            except ValueError: # Try without hour/minutes/seconds.
67+                raise validators.ValidationError, gettext('Enter a valid time in HH:MM or HH:MM:SS format.')
68+   
69     def get_db_prep_lookup(self, lookup_type, value):
70         if lookup_type == 'range':
71             value = [str(v) for v in value]