twitter bootstrap - Django 1.8 DateTimeInput storing as UnixTimestamp -


i have problem datetimeinput fields in django 1.8.

the following form used:

def __init__(self, *args, **kwargs):     """additional init function changing field behavior      """     super(updatemaintenanceform, self).__init__(*args, **kwargs)     self.fields['name'].widget = forms.textinput(attrs={'class': 'form-control'})     self.fields['description'] = forms.charfield(         required=false, widget=forms.textarea(attrs={'class': 'form-control'})     )     self.fields['active_since'] = forms.datetimefield(         input_formats=['%d.%m.%y %h:%m'], widget=forms.datetimeinput(attrs={'class': 'form-control'})     )     self.fields['active_till'] = forms.datetimefield(         input_formats=['%d.%m.%y %h:%m'], widget=forms.datetimeinput(attrs={'class': 'form-control'})     )     self.initial['active_since'] = datetime.fromtimestamp(self.initial['active_since']).strftime("%d.%m.%y %h:%m")     self.initial['active_till'] = datetime.fromtimestamp(self.initial['active_till']).strftime("%d.%m.%y %h:%m") 

i'm storing dates unix timestamps in database. when adding date got error message 'enter valid date/time'. stepping debugger found error raises to_python method defined integerfields. problem clear me, can not add datetime object integer field. not want storing dates datetimes database. therefore need modify to_python method convert datetime object unix timestamp. best way this? write custom field model or there way manipulating widget using own to_python method?

solved issue subclassing models.integerfield , adding own to_python conversion:

class unixdatetime(models.integerfield):

def to_python(self, value):      if value none:         return value     if isinstance(value, int):         return value     if isinstance(value, datetime):         return mktime(value.timetuple())     else:         raise exceptions.validationerror(             self.error_messages['invalid'], code='invalid', params={'value': value},         ) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -