有一个内联表单类:
class ItemColorSelectForm(forms.ModelForm): def __init__(self,*args,**kwargs): super(ItemColorSelectForm,self).__init__(*args,**kwargs) #here i need current object
内联类:
class ItemColorSelectInline(generic.GenericTabularInline): model = ColorSelect extra = 1 form = ItemColorSelectForm
管理员班
class ItemAdmin(admin.ModelAdmin): inlines = [ItemColorInline,]
问题:如何在ItemColorSelectForm中获取当前对象.
打印kwargs返回:
{'auto_id': u'id_%s','prefix': u'catalog-colorselect-content_type-object_id-__prefix__','empty_permitted': True}
解决方法
目前接受的解决方案不是线程安全的如果您关心线程安全,请永远不要将实例分配给静态类属性.
线程安全解决方案是:
对于Django 1.7< 1.9(可能是早期版本,不清楚):
from django.utils.functional import cached_property def get_formset(self,**kwargs): FormSet = super(InlineAdmin,self).get_formset(*args,**kwargs) class ProxyFormSet(FormSet): def __init__(self,**kwargs): self.instance = kwargs['instance'] super(ProxyFormSet,**kwargs) @cached_property def forms(self): kwargs = {'instance': self.instance} forms = [self._construct_form(i,**kwargs) for i in xrange(self.total_form_count())] return forms return ProxyFormSet
从Django 1.9> =它也可以传递form_kwargs:
def get_formset(self,**kwargs): form_kwargs = kwargs.pop('form_kwargs',{}) form_kwargs['instance'] = kwargs['instance'] super(ProxyFormSet,self).__init__( *args,form_kwargs=form_kwargs,**kwargs) return ProxyFormSet
以上解决方案将以模型形式提供实例kwarg:
class InlineForm(forms.ModelForm): def __init__(self,**kwargs): super(InlineForm,**kwargs) print('instance',kwargs['instance'])