子类化 django 字段
当我尝试创建新的模型字段类型 - postgres time[] 时遇到问题。
感谢 psycopg2,数据库中的信息已准备就绪并转换为 python 列表。
该列表看起来像这样的想法:
[[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)]]
现在我可以创建小部件,它使用 JavaScript 将其转换为时间。但在我看来,在 python 方面可以更快地完成,并且由于各种时间选择器小部件以 %H:%M:%S 格式处理时间,因此以相同格式获取和提供数据看起来是明智的。将数据转换回字符串,即“datetime.time(0, 0)”看起来也不太好。
所以,这一切让我想问 - 数据库读取后调用 django.db.models.fields.Field 类的什么样的方法?来自 https://docs.djangoproject.com/en/dev/howto /custom-model-fields/ 它看起来应该是 Field.to_python,但是当我向我的字段添加如下方法时
def to_python(self, value):
return 'xxx'
,我仍然看到之前在我的输入中添加的日期时间列表而不是“xxx”
有人可以吗请帮我解决这个问题:)
艾伦
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? from https://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 :)
Alan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在网上搜索后,我找到了一个解决方案 - 信号。
在类范围之外:
在字段范围内
基本上就是这样。
After digging around in the web i found a solution - signals.
outside of class scope:
inside field scope
Basically thats it.