python - How to convert Django's TimeField to Postgres' timestamp with time zone -
in django app, program gets time of event user, user inputs 10:30 time of event. program uses modelformset_factory create whole bunch of forms:
#forms.py class eventform(modelform): class meta: model = event fields = ['entry', 'time', 'work'] localized_fields = ('time', ) widgets = { 'work': textarea(attrs = {'cols': 40, 'rows': 8}), } #models.py class event(models.model): ''' each day there many events, e.g. @ 10 am, framer orders material, etc. ''' entry = models.foreignkey(entry) time = models.timefield() work = models.textfield() #views.py: eventformset = modelformset_factory( event, form = eventform, exclude = ('entry',),extra = 5) eventset = self.eventformset(request.post) all_errors = eventset.errors # errors returns [{}, {}, {}, {}, {}], data free of error. try: eventset.is_valid() except validationerror: return render(request, self.template_name, {'form': self.day_log(initial = self.initial_values), 'eventforms': self.event_formset}) events_instances = eventset.save(commit = false) instance in events_instances: if instance.time: event_date = datetime.date(the_year, the_month, the_day) # naive date event_time = instance.time # naive time naive_event_time = timezone.is_naive(event_time) # checking naivaty event_datetime = datetime.datetime.combine(event_date, event_time) # naive datetime object is_aware_event_datetime = timezone.is_aware(event_datetime) # checking see if datetime object aware or naive event_aware_datetime = datetime.datetime(the_year, the_month, the_day, instance.time.hour, instance.time.minute, instance.time.second, tzinfo = pytz.utc) # making aware time is_aware_event_aware_datetime = timezone.is_aware(event_aware_datetime) # making sure event_aware_datetime indeed aware instance.time = event_aware_datetime awareness_test = timezone.is_aware(instance.time) # making sure instance.time aware eventset.save() # here exception thrown.
the django app fails save time data postgres. exception type dataerror exception value:
invalid input syntax type timestamp time zone: line 1: ...y_event" ("entry_id", "time", "work") values (14, '18:43:04....
with ^ pointing @ ' of '18:
edit: here database:
create table site_activity_event ( id serial not null, entry_id integer not null, "time" time without time zone not null, work text not null, constraint site_activity_event_pkey primary key (id), constraint site_activity_event_entry_id_fkey foreign key (entry_id) references site_activity_entry (id) match simple on update no action on delete no action deferrable deferred )
i did sqlall app ensure data type of model field correspond of app , there. seems postgres issue, django supposed take care of pesky interface issues postgres (https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/#migration-guide), makes me think missing something. appreciated.
well, "18:43:04" valid timestamp timezone? think specifies unique point in time? if have uncovered major bug in postgresql's date/time handling. if not, should either supply valid timestamp or use correct type in database - can't without knowing more these events.
Comments
Post a Comment