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
.
print kwargs
return:
{'auto_id': u'id_%s', 'prefix': u'catalog-colorselect-content_type-object_id-__prefix__', 'empty_permitted': True}
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):
As of Django >= 1.9 it's also possible to pass form_kwargs:
Above solutions will make an instance kwarg available in the model form:
To fix: currently accepted solution not safe in multi-thread mode
Arti's solution works, another better option could be:
So, there is no need to override the
__init__()
in form anymore, neither the current object.works in Django 2.1.7
Solution: Override the formset method in Inline class