django内联问题

时间:2009-05-26 06:39:48

标签: django django-models django-admin django-forms

我制作了一个名为Fooinline的内联版。这个内联在Django 1.02中工作正常,但是当我升级到Django 1.1时它开始出错:

**TypeError at /admin/casd/aaas/4028cb901dd9720a011deadd85e8007f/
__init__() got an unexpected keyword argument 'request'**

我的Fooinline代码是:

class FooInline(InlineModelAdmin):

 template = 'admin/edit_inline/tabular2.html'

 def formfield_for_dbfield(self,db_field, **kwargs):
#    if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
#        BaseModelAdmin.formfield_for_dbfield(self, db_field, **kwargs)
#    else:
     kwargs['widget'] = FlatTextWidget
     return db_field.formfield(**kwargs)

我的FlatTextWidget

class FlatTextWidget(HiddenInput):
 def __init__(self, attrs=None):
     if attrs is not None:
         self.attrs = attrs.copy()
         super(Widget, self).__init__(attrs)
     else:
         self.attrs = {}
         super(Widget, self).__init__()

def render(self, name, value, attrs=None):
    real_field = super(HiddenInput, self).render(name, value, attrs)
    x = value
    if x is None:
        x = ''
    return mark_safe( '%s%s' % (real_field, x) )

def _has_changed(self, initial, data):
    return False

我将Fooinline称为

 class XYZInline(Fooinline):
    model=XYZ

1 个答案:

答案 0 :(得分:2)

错误在formfield_for_dbfield中,应该是:

def formfield_for_dbfield(self, db_field, **kwargs):
    kwargs['widget'] = FlatTextWidget
    return super(FooInline, self).formfield_for_dbfield(db_field,**kwargs)

查看contrib/admin/options.py#L71

中的原始功能