Have a inline form class:
有一个内联表单类:
class ItemColorSelectForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ItemColorSelectForm, self).__init__(*args, **kwargs)
#here i need current object
Inline class:
class ItemColorSelectInline(generic.GenericTabularInline):
model = ColorSelect
extra = 1
form = ItemColorSelectForm
Admin class
class ItemAdmin(admin.ModelAdmin):
inlines = [ItemColorInline,]
Question: how can a get current object in ItemColorSelectForm
.
问题:如何在ItemColorSelectForm中获取当前对象。
print kwargs
return:
打印kwargs返回:
{'auto_id': u'id_%s', 'prefix': u'catalog-colorselect-content_type-object_id-__prefix__', 'empty_permitted': True}
2 个解决方案
#1
6
Currently accepted solution is not thread safe. If you care about thread safety, never, ever assign an instance to a static class property.
目前接受的解决方案不是线程安全的如果您关心线程安全,请永远不要将实例分配给静态类属性。
Thread safe solutions are:
线程安全解决方案是:
For Django 1.7 < 1.9 (possibly earlier versions, unclear):
对于Django 1.7 <1.9(可能是早期版本,不清楚):
from django.utils.functional import cached_property
def get_formset(self, *args, **kwargs):
FormSet = super(InlineAdmin, self).get_formset(*args, **kwargs)
class ProxyFormSet(FormSet):
def __init__(self, *args, **kwargs):
self.instance = kwargs['instance']
super(ProxyFormSet, self).__init__(*args, **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
As of Django 1.9 >= it's also possible to pass form_kwargs:
从Django 1.9开始> =也可以传递form_kwargs:
def get_formset(self, *args, **kwargs):
FormSet = super(InlineAdmin, self).get_formset(*args, **kwargs)
class ProxyFormSet(FormSet):
def __init__(self, *args, **kwargs):
form_kwargs = kwargs.pop('form_kwargs', {})
form_kwargs['instance'] = kwargs['instance']
super(ProxyFormSet, self).__init__(
*args, form_kwargs=form_kwargs, **kwargs)
return ProxyFormSet
Above solutions will make an instance kwarg available in the model form:
以上解决方案将以模型形式提供实例kwarg:
class InlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(InlineForm, self).__init__(*args, **kwargs)
print('instance', kwargs['instance'])
#2
3
Solution: Override the formset method in Inline class
解决方案:覆盖Inline类中的formset方法
def get_formset(self, request, obj=None, **kwargs):
InlineForm.obj = obj
return super(InlineAdmin, self).get_formset(request, obj, **kwargs)
#1
6
Currently accepted solution is not thread safe. If you care about thread safety, never, ever assign an instance to a static class property.
目前接受的解决方案不是线程安全的如果您关心线程安全,请永远不要将实例分配给静态类属性。
Thread safe solutions are:
线程安全解决方案是:
For Django 1.7 < 1.9 (possibly earlier versions, unclear):
对于Django 1.7 <1.9(可能是早期版本,不清楚):
from django.utils.functional import cached_property
def get_formset(self, *args, **kwargs):
FormSet = super(InlineAdmin, self).get_formset(*args, **kwargs)
class ProxyFormSet(FormSet):
def __init__(self, *args, **kwargs):
self.instance = kwargs['instance']
super(ProxyFormSet, self).__init__(*args, **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
As of Django 1.9 >= it's also possible to pass form_kwargs:
从Django 1.9开始> =也可以传递form_kwargs:
def get_formset(self, *args, **kwargs):
FormSet = super(InlineAdmin, self).get_formset(*args, **kwargs)
class ProxyFormSet(FormSet):
def __init__(self, *args, **kwargs):
form_kwargs = kwargs.pop('form_kwargs', {})
form_kwargs['instance'] = kwargs['instance']
super(ProxyFormSet, self).__init__(
*args, form_kwargs=form_kwargs, **kwargs)
return ProxyFormSet
Above solutions will make an instance kwarg available in the model form:
以上解决方案将以模型形式提供实例kwarg:
class InlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(InlineForm, self).__init__(*args, **kwargs)
print('instance', kwargs['instance'])
#2
3
Solution: Override the formset method in Inline class
解决方案:覆盖Inline类中的formset方法
def get_formset(self, request, obj=None, **kwargs):
InlineForm.obj = obj
return super(InlineAdmin, self).get_formset(request, obj, **kwargs)