如何在管理站点中的输入表单之前放置​​一些文本?

时间:2012-05-30 11:31:06

标签: django django-forms django-admin

如何在文本输入之前放置一些文本,例如用户ID?我需要这样的东西

mockup

代码是DecimalField

我应该创建一个全新的小部件吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

您需要自定义ModelAdmin类和表单小部件,例如

from django import forms
from django.utils.safestring import mark_safe

class ModelAdminFoo(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
         request = kwargs['request']
         if db_field.name == 'code':
             # You could put following customized widget elsewhere
             class Widget(forms.TextInput):
                 def render(self, *args, **kwargs):
                     return mark_safe('%d - ' % request.user.pk) + super(Widget, self).render(*args, **kwargs)
             kwargs['widget']= Widget()
         return super(ModelAdminFoo, self).formfield_for_dbfield(db_field, **kwargs)