如何在文本输入之前放置一些文本,例如用户ID?我需要这样的东西
代码是DecimalField
我应该创建一个全新的小部件吗?
非常感谢答案 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)