DateTimeField with microseconds

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class DateTimeWithUsecsField(forms.DateTimeField):
    def clean(self, value):
        if value and '.' in value: 
            value, usecs = value.rsplit('.', 1) # rsplit in case '.' is used elsewhere
            usecs += '0'*(6-len(usecs)) # right pad with zeros if necessary
            try:
                usecs = int(usecs) 
            except ValueError: 
                raise ValidationError('Microseconds must be an integer') 
        else: 
            usecs = 0 
        cleaned_value = super(DateTimeWithUsecsField, self).clean(value)
        if cleaned_value:
            cleaned_value = cleaned_value.replace(microsecond=usecs)
        return cleaned_value

More like this

  1. RequestFetchingMixin by eternicode 3 years, 2 months ago
  2. DropDownMultiple widget by marinho 5 years, 11 months ago
  3. SectionedForm by marinho 5 years, 10 months ago
  4. Getting dynamic model choices in newforms by ubernostrum 7 years, 1 month ago
  5. Tag that parses args and kwargs by fivethreeo 5 years, 6 months ago

Comments

(Forgotten your password?)