2012-05-30 41 views

回答

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) 
相關問題