Googled alot and figured it out.
def contribute_to_class(self, cls, name):
super(MyFieldName, self).contribute_to_class(cls, name)
signals.post_init.connect(self.post_init, cls, True)
and self.post_init:
def post_init(self, **kwargs):
logger.debug('postinig')
instance = kwargs['instance']
value = self.value_from_object(instance)
if value:
setattr(instance, self.attname, loads(value))
else:
setattr(instance, self.attname, None)
Where loads does the actual converting.
Alan
On Sep 8, 11:37 am, zayatzz <alan.kesselm...@gmail.com> wrote:
> I have a problem when i try to create new model field type - postgres
> time[].
>
> Thanks to psycopg2 the information from database is ready out and
> converted into python list.
>
> the list looks like that thought:
>
> [[datetime.time(0, 0), datetime.time(23, 59, 59)], [datetime.time(0,
> 0), datetime.time(23, 59, 59)], [datetime.time(0, 0),
> datetime.time(23, 59, 59)], [datetime.time(0, 0), datetime.time(23,
> 59, 59)], [datetime.time(0, 0), datetime.time(23, 59, 59)],
> [datetime.time(0, 0), datetime.time(23, 59, 59)], [datetime.time(0,
> 0), datetime.time(23, 59, 59)]]
> Now i could create widget, that uses JavaScript to convert it into
> time. But it seems to me, that it could be done faster on python side
> and since all kinds of time picker widgets deal with time in %H:%M:%S
> format it looks wise to both get and give data in same format.
> Converting data back to string, which is 'datetime.time(0, 0)' does
> not look very nice either.
>
> so, all that brings me to asking - what kind of method of
> django.db.models.fields.Field class is called after database read?
> fromhttps://docs.djangoproject.com/en/dev/howto/custom-model-fields/
> it looks like it should be Field.to_python, but when i add method
> like:
>
> def to_python(self, value):
> return 'xxx'
> to my field, then i still see previously added list of datetimes in my
> input not 'xxx'
>
> Can someone help me sort this out please :) - the question is
> basically - how can i format the data read from database into desired
> format for displaying in formfield?
>
> Alan
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment