如何为Django模型字段创建别名? [英] How to create an alias for Django Model field?

查看:242
本文介绍了如何为Django模型字段创建别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Django模型有一个名为 updatedOn的日期时间字段,我需要在该模型上使用一个库函数来计算一些统计信息,但是该函数假定该日期时间字段名称为时间,这就是该函数的用法日期时间:

My Django Model has a datetime field named 'updatedOn', I need to use a library function on this model to calculate some statistics, but the function assumes the datetime field name to be 'time', here is how the function uses the datetime:

c = qset.filter(time__year=tt.year, time__month=tt.month, time__day=tt.day).count();

在不触摸库代码的情况下,如何创建别名时间来引用 updatedOn字段,以便可以使用该函数?

Without touching the library code, how can I create an alias 'time' to refer to 'updatedOn' field, so that I can use the function?

推荐答案

我并没有对此进行深入研究,但从直觉上讲,以下方法可能会让您入门。

I haven't looked into this very deeply, but intuitively, some of the following ways might get you started.

A 自定义管理器,其中包含修改后的 get_queryset 方法来查找 updatedOn 过滤时间时。

A custom manager with a modified get_queryset method to look for updatedOn when filtering for time.

可能可以创建自定义字段类型,仅作为对另一个字段的引用。

It might be possible to create a custom field type which only acts as a reference to another field.

模型对象的 _meta.fields 似乎包含以下字段的列表:在那个物体上。也许您可以尝试添加某种称为 time 的虚拟字段,该字段指的是 updatedOn 字段。

The model object's _meta.fields seems to contain the list of fields in that object. Maybe you could try adding some kind of dummy field called time, which refers to the updatedOn field.

这篇关于如何为Django模型字段创建别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆