2011-06-14 294 views

回答

1

不知道您要添加的东西,但,这是一个解决方案,我发现其他地方的SO改变的HTML一个FileField(在我的情况下,我想在ImageField中显示当前图像)。

换句话说,你可以作出这样的修改要自定义字段的HTML一个小部件:

# Widget that modifies the output of a FileField 
class OutputWidget(AdminFileWidget): 
    # Overloaded django magic 
    def render(self, name, value, attrs=None): 
     output = [] 
     # This is the place where we edit the output  
     if value and getattr(value, "url", None): 
      image_url = value.url 
      output.append(u' <a href="%s" target="_blank"><img src="%s" alt="%s" /></a>' % (image_url, image_url, image_url)) 
     output.append(super(AdminFileWidget, self).render(name, value, attrs)) 
     return mark_safe(u''.join(output)) 

# ModelAdmin class that is applied to the model 
class MyModelSettings(admin.ModelAdmin): 
    # Overloaded django magic 
    def formfield_for_dbfield(self, db_field, **kwargs): 
     # Look for the field we want to edit and register the widget with it 
     if db_field.name == 'nameOfFieldIWantToEdit': 
      request = kwargs.pop("request", None) 
      kwargs['widget'] = OutputWidget 
      return db_field.formfield(**kwargs) 
     return super(MyModelSettings,self).formfield_for_dbfield(db_field, **kwargs) 

# Register my overloaded settings with the model 
admin.site.register(MyModel, MyModelSettings) 

的代码去到您注册的模型admin.py。

0

我用Omokoli的解决方案上面,但使该字段使用个性化微件我所做的:

 
class MyModelAdminForm(forms.ModelForm): 
    class Meta: 
     model = get_model('myapp', 'mymodel') 
     widgets = { 
      'original_link': OutputWidget, 
     }