Django:如何更改 AdminTimeWidget 的选择
在管理中为 DateTimeField
呈现的 AdminTimeWidget
显示一个时钟图标,当您单击时,您可以选择:“现在午夜 6:00 中午”。
如何将这些选项更改为“16h 17h 18h”?
The AdminTimeWidget
rendered in admin for a DateTimeField
displays an icon of a clock and when you click you have the choice between: "Now Midnight 6:00 Noon".
How can I change these choices to "16h 17h 18h"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
克里斯有一个很好的答案。作为替代方案,您可以仅使用 javascript 来完成此操作。将以下 javascript 放置在您想要不同时间选项的页面上。
Chris has a great answer. As an alternative you could do this using just javascript. Place the following javascript on the pages where you want the different time options.
子类
AdminTimeWidget
以包含修改后的 DateTimeShortcuts.js(一秒钟即可完成),然后子类AdminSplitDateTime
以包含子类MyAdminTimeWidget
而不是默认 Django 之一:秘密武器位于 django/contrib/admin/media/js/admin/DateTimeShortcuts.js 中。这就是创建您要修改的列表的原因。复制此文件并将其粘贴到项目的
site_media/js/admin
目录中。您需要修改的相关代码位于第 85-88 行:只需根据您的喜好添加/删除/修改该段 javascript 即可。
最后,将新小部件附加到您喜欢的任何日期时间字段。您最好的选择可能是
ModelAdmin
上的formfield_overrides
属性:Subclass
AdminTimeWidget
to include a modified DateTimeShortcuts.js (get to that in a sec), then subclassAdminSplitDateTime
to include your subclassedMyAdminTimeWidget
instead of the default Django one:The secret sauce is in
django/contrib/admin/media/js/admin/DateTimeShortcuts.js
. This is what creates the list you want to modify. Copy this file and paste it into your project'ssite_media/js/admin
directory. The relevant code you need to modify is on lines 85-88:Simply add to/delete from/modify that bit of javascript to your heart's content.
Finally, attach your new widget to any DateTimeFields you like. Your best bet for that will probably be the
formfield_overrides
attribute onModelAdmin
:我尝试使用此方法,发现当表单上存在多个日期时间时,上述 JavaScript 不起作用。
这就是我所做的。
在我的 ModelAdmin 部分中,我添加了:
然后在 js 文件中:
注意:我必须放入 document.ready 中,因为我发现我无法控制脚本包含在页面中的位置(似乎已在默认值之前加载)日历 js 文件)。
I tried using this method and found the above javascript didn't work when multiple datetime's were present on the form.
here is what I did.
In my ModelAdmin section i added:
then in the js file:
Note: i had to put inside a document.ready because i found that i couldn't control where the script was included in the page (seems to have be loaded before the default calendar js files).
有更好的解决方案。阅读 DateTimeShortcuts.js 后 可以简化为:
然后将此代码添加到“static//time-shortcuts.js”中的 javascript 文件中,并将 Meta 添加到您的管理模型中:
There's better solution. After reading DateTimeShortcuts.js the can be simplified to:
Then add this code to the javascript file in 'static//time-shortcuts.js' and add Meta to your admin model:
我采用了一种更简单的方法,它对我有用。我只是使用以下代码向模型添加了选择:
class Class(Model):
程序=ForeignKey('程序')
当日时间 = TimeField(选择=(
(datetime.datetime.strptime('7:00 am', "%I:%M %p").time(), '7:00 am'),
(datetime.datetime.strptime('上午 8:00', "%I:%M %p").time(), '上午 8:00'),
(datetime.datetime.strptime('上午 9:00', "%I:%M %p").time(), '上午 9:00'),
(datetime.datetime.strptime('下午 6:00', "%I:%M %p").time(), '下午 6:00'),
(datetime.datetime.strptime('晚上 7:00', "%I:%M %p").time(), '晚上 7:00'),
(datetime.datetime.strptime('晚上 8:00', "%I:%M %p").time(), '晚上 8:00'),
(datetime.datetime.strptime('晚上 9:00', "%I:%M %p").time(), '晚上 9:00'),
))
希望这有帮助
I went with a much simpler approach and it worked for me. I simply added choices to my model using the following code:
class Class(Model):
program = ForeignKey('Program')
time_of_the_day = TimeField(choices=(
(datetime.datetime.strptime('7:00 am', "%I:%M %p").time(), '7:00 am'),
(datetime.datetime.strptime('8:00 am', "%I:%M %p").time(), '8:00 am'),
(datetime.datetime.strptime('9:00 am', "%I:%M %p").time(), '9:00 am'),
(datetime.datetime.strptime('6:00 pm', "%I:%M %p").time(), '6:00 pm'),
(datetime.datetime.strptime('7:00 pm', "%I:%M %p").time(), '7:00 pm'),
(datetime.datetime.strptime('8:00 pm', "%I:%M %p").time(), '8:00 pm'),
(datetime.datetime.strptime('9:00 pm', "%I:%M %p").time(), '9:00 pm'),
))
Hope this helps
通过 DateTimeShortcuts.overrideTimeOptions 函数覆盖 JS 仅适用于一种表单
(错误:子模型中时间的更改会影响父模型,因此您无法通过此小部件更改子模型表单中的时间字段)
如果您想使用带有内联的自定义时间选项:
在 /static/admin/js/ 中管理/DateTimeShortcuts.js
替换:
为:
Overriding JS by DateTimeShortcuts.overrideTimeOptions function works only with one form
( bug: the change of time in child model affects parent model, so you can't change timefield in child model form by this widget)
If you want use custom time options with inlines:
in /static/admin/js/admin/DateTimeShortcuts.js
replace:
by:
扩展@Bit68的答案,假设其他人可能想要多次创建定期间隔时间的列表,我创建了一个辅助函数来构建选择元组。 (我添加了一个新答案,因为这段代码太难在注释中遵循。)这适用于 Django 2.2。
请注意,这会创建一个选项下拉列表,它不会像 javascript 方法那样将选项添加到默认管理日期/时间小部件。
然后
time_of_the_day = models.TimeField(choices=get_time_choices())
Expanding on @Bit68's answer, assuming other folks might want to create lists of regularly spaced times more than once, I created a helper function to build a choices tuple. (I'm adding a new answer because this length of code is too hard to follow in a comment.) This works in Django 2.2.
Note that this creates a dropdown list of options, it doesn't add options to the default admin date/time widget as the javascript methods do.
Then
time_of_the_day = models.TimeField(choices=get_time_choices())
在检查了 DateTimeShortcuts.js 的源代码后,我发现很容易覆盖处理
DateTimeShortcuts.clockHours.default_
的函数并使其管理像16.5
这样的浮点数(对于“16 :30”)。我现在可以做更多事情,例如同步两个时间输入(因为我有两个 start_at 和 end_at 字段):After checking the source code of DateTimeShortcuts.js I found quite easy to overwrite the function that handles
DateTimeShortcuts.clockHours.default_
and make it manage also floats like16.5
(for "16:30"). I can now do much more, like synchronizing two time inputs (as I have two start_at and end_at fields):谢谢大家在这里提供的一些好建议。我得到了这个简单的解决方案。我将这个
admin.js
文件放在project/static/admin.js
中。该文件的内容是:然后在 admin.py 中我有这个:
现在,我在交易模型管理中的数据时间字段的小时钟小部件有以下选择:现在、午夜、市场开盘和市场收盘。
Thank you all for some good tips here. I got this simple solution working. I put this
admin.js
file inproject/static/admin.js
. The contents of that file are:Then in admin.py I have this:
And now my little clock widget for my data time field in the Trade model admin has these choices: Now, Midnight, Market Open, and Market Close.