我正在尝试修改现有的Django Mezzanine设置,以允许我在Markdown中发布博客。 Mezzanine有一个“核心”模型,其内容为HtmlField,其定义如下:
from django.db.models import TextField
class HtmlField(TextField):
"""
TextField that stores HTML.
"""
def formfield(self, **kwargs):
"""
Apply the class to the widget that will render the field as a
TincyMCE Editor.
"""
formfield = super(HtmlField, self).formfield(**kwargs)
formfield.widget.attrs["class"] = "mceEditor"
return formfield
问题来自mceEditor的widget.attrs [“class”]。我的想法是修补Blog对象上的Content字段
class BlogPost(Displayable, Ownable, Content):
def __init__(self, *args, **kwargs):
super(BlogPost, self).__init__(*args, **kwargs)
self._meta.get_field('content').formfield = XXX
我的问题是我的python技能不能胜任使用调用super
的lambda替换绑定方法的任务。
答案 0 :(得分:1)
您可以在管理员方法formfield_for_dbfield
中更改已使用的表单字段:
class BlogAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
field = super(BlogAdmin, self).formfield_for_dbfield(db_field, **kwargs)
if db_field.name == 'content':
field.widget = ....
field.widget.attrs['class'] = ...
return field
如果你真的想做猴子修补,它应该是这样的:
class BlogPost(Displayable, Ownable, Content):
def __init__(self, *args, **kwargs):
super(BlogPost, self).__init__(*args, **kwargs)
def formfield_new(self, *args, **kwargs):
# do here what you would like to do
return formfield
instancemethod = type(self._meta.get_field('content').formfield)
self._meta.get_field('content').formfield = instancemethod(formfield_new,
self, BlogPost)
答案 1 :(得分:1)
我意识到几个月前这个问题得到了回答,但是万一其他人遇到它,Mezzanine现在提供了完全修改WYSIWYG编辑器字段的能力。看看这里的文档:
http://mezzanine.jupo.org/docs/admin-customization.html#wysiwyg-editor