子类化 django 字段

发布于 2024-12-03 08:36:37 字数 1075 浏览 0 评论 0原文

当我尝试创建新的模型字段类型 - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2024-12-10 08:36:37

在网上搜索后,我找到了一个解决方案 - 信号。

在类范围之外:

def loads(value)
    #do value conversion here

在字段范围内

def contribute_to_class(self, cls, name):
    super(MyFieldName, self).contribute_to_class(cls, name)
    signals.post_init.connect(self.post_init, cls, True)

def post_init(self, **kwargs):
    instance = kwargs['instance']
    value = self.value_from_object(instance)
    if value:
        setattr(instance, self.attname, str(loads(value)))
    else:
        setattr(instance, self.attname, None)

基本上就是这样。

After digging around in the web i found a solution - signals.

outside of class scope:

def loads(value)
    #do value conversion here

inside field scope

def contribute_to_class(self, cls, name):
    super(MyFieldName, self).contribute_to_class(cls, name)
    signals.post_init.connect(self.post_init, cls, True)

def post_init(self, **kwargs):
    instance = kwargs['instance']
    value = self.value_from_object(instance)
    if value:
        setattr(instance, self.attname, str(loads(value)))
    else:
        setattr(instance, self.attname, None)

Basically thats it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文