更改 Django 管理中日期字段的默认小部件
如何更改 Django ADMIN 中 DateField 实例的默认小部件?
我知道如何为 ModelForm 执行此操作 - 如何更改 ModelForm 中所有 Django 日期字段的默认小部件? - 通过提供 formfield_callback 来检查字段类型,然后提供带有新小部件的表单字段。
然而,我的一些 ModelAdmin 可能已经有自定义 ModelForm,有些还没有。我不想在有日期字段的地方提供/更改 ModelForm(这就是重点)。
不过,可用于子类化的自定义 ModelAdmin 就可以了。模型管理员如何更改其表单的日期字段,而不管是否提供了自定义模型表单?
也许 ModelAdmin 具有 get_form() 调用其超级然后设置表单的 formfield_callback ?似乎有点复杂,不确定时间,而且如果任何 ModelAdmin 需要进一步定制,则需要注意三件事......
PS:我在 https://stackoverflow.com/questions/8484811/jquery-datepicker-in-django-admin 是由于是上述 ModelForm 问题的“精确重复”而被错误地关闭。我希望现在差异已经清楚了。
顺便说一句,我个人的动机是 django admin 默认日期选择器的年份导航。生日真的很糟糕......:-) 是的,有一张三年前的票 https://code. djangoproject.com/ticket/9388
How can I change the default widget for DateField instances in Django ADMIN?
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.
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).
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?
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...
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够在 Django 管理中自定义默认日期小部件。我遵循了两个教程(来自 简单胜于复杂和Django 自定义小部件)
widgets.py
的文件中编写自定义小部件请注意,您可能需要的 CSS 和 JS 文件应该是添加到内部
Media
类中,如上面的代码所示。在本例中,它为自定义日期选择器添加样式。template_name
的值相匹配:formfield_overrides
)。完成此操作后,您将在 Django 管理中拥有一个用于日期的自定义小部件。请记住,这只是一个指南,您可以通过查看我指出的两个教程来进一步了解。
注意:此小部件的实现还支持国际化。
I was able to customize the default date widget in the Django admin. I followed two tutorials (from Simple is Better than Complex and Django Custom Widget)
widgets.py
inside your Django appNotice that the CSS and JS files that you may need should be added in the internal
Media
class as shown in the code above. In this case it is adding styles for the custom date picker.app_name/templates/app_name/widgets/custom_datepicker.html
or make sure that this template matches the value oftemplate_name
in your custom widget:formfield_overrides
)Once this is completed, you will have a custom widget for dates in the Django admin. Keep in mind that this is just a guide, you can go further by reviewing the two tutorials I indicated.
Note: the implementation of this widget is also supporting internationalization.
ModelAdmin 只能更改它呈现的表单的小部件,因此很难覆盖传递给它的自定义表单的小部件 - 至少我想不出一种干净的方法来做到这一点。
我可以为您提供一些选项,具体取决于您想要定制的方式/内容。
修改为小部件发挥作用的 JavaScript。如果您的更改可以在那里处理,这似乎很容易。只需提供您自己的
js/calendar.js
和js/admin/DateTimeShortcuts.js
,就像覆盖管理员模板一样。这还有一个额外的好处,那就是升级友好;因为您没有修改 django 代码库;对于使用该小部件的所有表单,更改也会立即生效。自定义内置 django 小部件来执行您想要的操作。这样做的好处是可以立即工作,无需对现有代码进行任何修改,但除非您小心修补源代码,否则升级不友好。源代码位于 django.contrib.admin.widgets
提供您自己的自定义小部件 - 但随后您又回到了浏览代码并使用您的自定义小部件手动覆盖所有小部件实例的问题.
使用类似 grappelli 的内容并修改 templates/js 来执行您想要的操作。这里的额外好处是,您将免费获得许多其他功能(例如拥有仪表板的能力,如果您将其插入 django-admin-tools)。
django 在他们的管理中广泛使用 jquery,并且他们有自己的 jquery 命名空间(
django.jQuery
);因此,如果您打算做一些自定义小部件,请确保使用正确的命名空间以避免奇怪的问题。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.
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
andjs/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.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.
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 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.