1

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

小样

代码是一个 DecimalField

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

非常感谢

4

1 回答 1

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)
于 2012-05-30T15:51:10.433 回答