Ticket #3408: __init__.py.diff
File __init__.py.diff, 2.7 KB (added by , 18 years ago) |
---|
-
__init__.py
437 437 Field.__init__(self, verbose_name, name, **kwargs) 438 438 439 439 def to_python(self, value): 440 if value is None: 441 if self.null: 442 return value 443 else: 444 raise validators.ValidationError, gettext_lazy("This field cannot be null.") 440 445 if isinstance(value, datetime.datetime): 441 446 return value.date() 442 447 if isinstance(value, datetime.date): … … 478 483 return override 479 484 else: 480 485 return self.editable or self.auto_now or self.auto_now_add 481 486 482 487 def get_db_prep_save(self, value): 483 488 # Casts dates into string format for entry into database. 484 if value is not None: 489 # MINE: if value is not None and type(value) != type(''): 490 if isinstance(value, datetime.datetime): 491 value = value.date().strftime('%Y-%m-%d') 492 elif isinstance(value, datetime.date): 485 493 value = value.strftime('%Y-%m-%d') 486 494 return Field.get_db_prep_save(self, value) 487 495 … … 499 507 500 508 class DateTimeField(DateField): 501 509 def to_python(self, value): 510 if value is None: 511 if self.null: 512 return value 513 else: 514 raise validators.ValidationError, gettext_lazy("This field cannot be null.") 502 515 if isinstance(value, datetime.datetime): 503 516 return value 504 517 if isinstance(value, datetime.date): … … 782 795 if auto_now or auto_now_add: 783 796 kwargs['editable'] = False 784 797 Field.__init__(self, verbose_name, name, **kwargs) 785 798 799 def to_python(self, value): 800 if value is None: 801 if self.null: 802 return value 803 else: 804 raise validators.ValidationError, gettext_lazy("This field cannot be null.") 805 if isinstance(value, datetime.time): 806 return value 807 if isinstance(value, datetime.datetime): 808 return value.time() 809 try: # Seconds are optional, so try converting seconds first. 810 return datetime.time(*time.strptime(value, '%H:%M:%S')[3:6]) 811 except ValueError: 812 try: # Try without seconds. 813 return datetime.time(*time.strptime(value, '%H:%M')[3:5]) 814 except ValueError: # Try without hour/minutes/seconds. 815 raise validators.ValidationError, gettext('Enter a valid time in HH:MM or HH:MM:SS format.') 816 786 817 def get_db_prep_lookup(self, lookup_type, value): 787 818 if lookup_type == 'range': 788 819 value = [str(v) for v in value]