更改Django管理员中日期字段的默认小部件 [英] Change the default widget for date fields in Django admin

查看:191
本文介绍了更改Django管理员中日期字段的默认小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Django ADMIN中更改DateField实例的默认小部件?

How can I change the default widget for DateField instances in Django ADMIN?

我知道如何为ModelForm执行此操作 - 如何更改默认窗口小部件ModelForm中的所有Django日期字段? - 通过提供formfield_callback来检查字段类型,然后为新窗口小部件提供一个表单域。

I am aware of how to do this for a ModelForm - How do you change the default widget for all Django date fields in a ModelForm? - by providing formfield_callback that checks the field type and then provides a form field with the new widget.

然而,一些我的ModelAdmin可能已经有一个自定义的ModelForm,有些没有。我不想提供/更改ModelForm,只要有一个日期字段(这就是要点)。

However, some of my ModelAdmin's may already have a custom ModelForm, some haven't. I don't want to provide/change a ModelForm wherever there's a date field (that's the point).

可以用于子类化的自定义ModelAdmin可以,虽然。 ModelAdmin如何更改其窗体的日期字段,而不管是否提供了自定义ModelForm?

A custom ModelAdmin which could be used for subclassing would be ok, though. How can a ModelAdmin change the date fields of its form, independent of whether a custom ModelForm has been provided or not?

也许一个ModelAdmin与get_form()调用其超级设置表单的formfield_callback?似乎有点复杂,不知道时机,还有三件事要注意,如果任何ModelAdmin需要进一步的定制...

Maybe a ModelAdmin with get_form() calling its super then setting the form's formfield_callback? Seems a little convoluted, not sure about the timing, and also then there's three things to be aware of if any ModelAdmin needs further customization...

PS:我是在 https://stackoverflow.com/questions/8484811/jquery-datepicker-in-django -admin 被错误地关闭,作为上述ModelForm问题的确切重复。我希望现在有所区别。

P.S.: I'm asking this after https://stackoverflow.com/questions/8484811/jquery-datepicker-in-django-admin was erroneously closed for being an "exact duplicate" of the above-mentioned ModelForm question. I hope the difference is now clear.

我的个人动机是django管理员的默认日期戳的年度导航。对于生日真的很糟糕... :-)是的,有一张三岁的老虎机票 https:// code。 djangoproject.com/ticket/9388

Btw my personal motivation is the year navigation of django admin's default datepicker. Really bad for birthdays... :-) yes there's a three-year old ticket https://code.djangoproject.com/ticket/9388

推荐答案

ModelAdmin只能更改其呈现的窗体小部件,所以它将很难覆盖传递给它的自定义表单的小部件 - 至少我不能想到一个干净的方式来实现。

ModelAdmin can only change the widget of the form it renders, so it would be difficult to override the widgets of a custom form passed to it - at least I can't think of a clean way to do it.

我可以看到几个您可以根据自己的需要选择哪些选项。

I can see a few options for you, depending on how/what you want to customize.


  1. 修改对该小部件有魔力的javascript。如果您的更改可以在那里处理,这似乎很容易。只需提供您自己的 js / calendar.js js / admin / DateTimeShortcuts.js 管理员的模板这样做有助于升级友好;因为你没有修改django代码库;对于使用窗口小部件的所有窗体也可以立即进行更改。

  1. Modify the javascript that does the magic for the widget. This seems easy enough if your change can be handled there. Simply provide your own js/calendar.js and js/admin/DateTimeShortcuts.js the same way you would override the admin's templates. This has the added bonus of being upgrade friendly; since you are not modifying the django code base; also the change would be immediate for all forms that use the widget.

自定义内置的django窗口小部件以执行所需的操作。这有利于即时工作,无需对现有代码进行任何修改,但除非您对修补源很小心,否则不会升级友好。来源在 django.contrib.admin.widgets

Customize the built-in django widget to do what you want. This has the benefit of instantly working without any modifications of your existing code, but is not upgrade friendly unless you are careful about patching sources. The source is at django.contrib.admin.widgets

提供您自己的自定义窗口小部件 - 但是您可以回到执行代码的问题,并用自定义代码手动覆盖所有窗口小部件实例。

Provide your own custom widget - but then you are back to the problem of going through the code and manually overriding all widget instances with your custom one.

使用类似 grappelli 并修改templates / js来做你想要的。这里增加的奖励是,您将免费获得大量其他功能(如拥有仪表板的功能,如果您使用 django-admin-tools )。

Use something like grappelli and modify the templates/js to do what you want. Added bonus here is that you'll get lots of other functionality for free (like the ability to have a dashboard, if you plug it in with django-admin-tools).

django广泛使用jquery在他们的管理员中,他们有自己的jquery命名空间( django.jQuery );所以如果你打算做一些自定义的小部件,请确保使用正确的命名空间来避免出现奇怪的问题。

django uses jquery extensively in their admin and they have their own jquery namespace (django.jQuery); so if you plan on doing some custom widgets make sure you use the right namespace to avoid strange problems.

这篇关于更改Django管理员中日期字段的默认小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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