[Django] #27240: Docs - Passing custom parameters to formset forms in admin

19 views
Skip to first unread message

Django

unread,
Sep 19, 2016, 1:01:14 AM9/19/16
to django-...@googlegroups.com
#27240: Docs - Passing custom parameters to formset forms in admin
-----------------------------+---------------------------------------------
Reporter: arogachev | Owner: nobody
Type: | Status: new
Uncategorized |
Component: | Version: 1.10
contrib.admin |
Severity: Normal | Keywords: admin, form, formset, parameter
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------------
I have `Enterprise` model and related model `Attachment`. Attachments are
managed using formsets in both frontend and backend.

I use custom form to handle attachments, and it uses additional `user`
kwarg:

{{{#!python
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

I need to fill this parameter with `request.user` value. In frontend I use
the method recommended in
[https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-
custom-parameters-to-formset-forms docs]

{{{#!python
enterprise = Enterprise()
AttachmentFormSet = generic_inlineformset_factory(Attachment,
form=AttachmentForm, max_num=3, validate_max=True)
if request.method == 'POST':
form = EnterpriseForm(request.POST, request.FILES,
instance=enterprise, user=request.user)
attachment_formset = AttachmentFormSet(request.POST, request.FILES,
instance=enterprise, form_kwargs = {'user': request.user})
# ...
else:
form = EnterpriseForm(instance=enterprise, user=request.user)
attachment_formset = AttachmentFormSet(instance=enterprise,
form_kwargs = {'user': request.user})
# ...
}}}

But docs do not cover how achieve the same thing in backend using Django
Admin.

In admin I have:

{{{#!python
class AttachmentInline(GenericTabularInline):
model = Attachment
form = AttachmentForm
readonly_fields = ('is_image', 'user', 'created_at')
max_num = 3

class EnterpriseAdmin(MyCompareVersionAdmin):
inlines = [AttachmentInline]
# ...
}}}

It's unclear where and how we need to grab and pass `request.user`
parameter to `AttachmentForm`. After some trial and error, I ended up with
this workaround:

{{{#!python
class EnterpriseAdmin(MyCompareVersionAdmin):
inlines = [AttachmentInline]

def save_related(self, request, form, formsets, change):
AttachmentForm.user = request.user
super(MyCompareVersionAdmin, self).save_related(request, form,
formsets, change)
}}}

Related modifications in `AttachmentForm`:

{{{#!python
user = None

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
if user:
self.user = user
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

So I turned `user` to class attribute and set it from kwargs only if it's
passed and it's not `None`.

It works, but I don't like this approach and It looks like a hack for me.

I think the solution on how to properly do it should be added to the docs
in the same section along with frontend solution.

--
Ticket URL: <https://code.djangoproject.com/ticket/27240>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 19, 2016, 1:04:17 AM9/19/16
to django-...@googlegroups.com
#27240: Docs - Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by arogachev):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

New description:

I have `Enterprise` model and related model `Attachment`. Attachments are
managed using formsets in both frontend and backend.

I use custom form to handle attachments, and it uses additional `user`
kwarg:

{{{#!python
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

I need to fill this parameter with `request.user` value. In frontend I use
the method recommended in
[https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

custom-parameters-to-formset-forms docs] (code omitted for brevity).

{{{#!python
enterprise = Enterprise()
AttachmentFormSet = generic_inlineformset_factory(Attachment,
form=AttachmentForm, max_num=3, validate_max=True)
if request.method == 'POST':

attachment_formset = AttachmentFormSet(request.POST, request.FILES,
instance=enterprise, form_kwargs = {'user': request.user})

else:

In admin I have:

Related modifications in `AttachmentForm`:

{{{#!python
user = None

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:1>

Django

unread,
Sep 19, 2016, 1:05:01 AM9/19/16
to django-...@googlegroups.com
#27240: Docs - Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by arogachev:

Old description:

> I have `Enterprise` model and related model `Attachment`. Attachments are
> managed using formsets in both frontend and backend.
>
> I use custom form to handle attachments, and it uses additional `user`
> kwarg:
>
> {{{#!python
> def __init__(self, *args, **kwargs):
> self.user = kwargs.pop('user', None)
> super(AttachmentForm, self).__init__(*args, **kwargs)
> }}}
>
> I need to fill this parameter with `request.user` value. In frontend I
> use the method recommended in
> [https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

> custom-parameters-to-formset-forms docs] (code omitted for brevity).


>
> {{{#!python
> enterprise = Enterprise()
> AttachmentFormSet = generic_inlineformset_factory(Attachment,
> form=AttachmentForm, max_num=3, validate_max=True)
> if request.method == 'POST':

> attachment_formset = AttachmentFormSet(request.POST, request.FILES,
> instance=enterprise, form_kwargs = {'user': request.user})

> else:

New description:

I have `Enterprise` model and related model `Attachment`. Attachments are
managed using formsets in both frontend and backend.

I use custom form to handle attachments, and it uses additional `user`
kwarg:

{{{#!python
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

I need to fill this parameter with `request.user` value. In frontend I use
the method recommended in
[https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

custom-parameters-to-formset-forms docs] (code omitted for brevity).

{{{#!python
enterprise = Enterprise()
AttachmentFormSet = generic_inlineformset_factory(Attachment,
form=AttachmentForm, max_num=3, validate_max=True)
if request.method == 'POST':

attachment_formset = AttachmentFormSet(request.POST, request.FILES,
instance=enterprise, form_kwargs = {'user': request.user})

else:


attachment_formset = AttachmentFormSet(instance=enterprise,
form_kwargs = {'user': request.user})
}}}

But docs do not cover how achieve the same thing in backend using Django
Admin.

In admin I have:

Related modifications in `AttachmentForm`:

{{{#!python
user = None

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:2>

Django

unread,
Sep 19, 2016, 1:06:38 AM9/19/16
to django-...@googlegroups.com
#27240: Docs - Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by arogachev:

Old description:

> I have `Enterprise` model and related model `Attachment`. Attachments are


> managed using formsets in both frontend and backend.
>
> I use custom form to handle attachments, and it uses additional `user`
> kwarg:
>
> {{{#!python
> def __init__(self, *args, **kwargs):
> self.user = kwargs.pop('user', None)
> super(AttachmentForm, self).__init__(*args, **kwargs)
> }}}
>
> I need to fill this parameter with `request.user` value. In frontend I
> use the method recommended in
> [https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

> custom-parameters-to-formset-forms docs] (code omitted for brevity).


>
> {{{#!python
> enterprise = Enterprise()
> AttachmentFormSet = generic_inlineformset_factory(Attachment,
> form=AttachmentForm, max_num=3, validate_max=True)
> if request.method == 'POST':

> attachment_formset = AttachmentFormSet(request.POST, request.FILES,
> instance=enterprise, form_kwargs = {'user': request.user})

> else:


> attachment_formset = AttachmentFormSet(instance=enterprise,
> form_kwargs = {'user': request.user})
> }}}
>

New description:

I have `Enterprise` model and related model `Attachment`. Attachments are
managed using formsets in both frontend and backend.

I use custom form to handle attachments, and it uses additional `user`
kwarg:

{{{#!python
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

I need to fill this parameter with `request.user` value. In frontend I use
the method recommended in
[https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

custom-parameters-to-formset-forms docs] (code omitted for brevity).

{{{#!python
enterprise = Enterprise()
AttachmentFormSet = generic_inlineformset_factory(Attachment,
form=AttachmentForm, max_num=3, validate_max=True)
if request.method == 'POST':

attachment_formset = AttachmentFormSet(request.POST, request.FILES,
instance=enterprise, form_kwargs = {'user': request.user})

else:


attachment_formset = AttachmentFormSet(instance=enterprise,
form_kwargs = {'user': request.user})
}}}

But docs do not cover how achieve the same thing in backend using Django
Admin.

In admin I have:

{{{#!python
class AttachmentInline(GenericTabularInline):
model = Attachment
form = AttachmentForm

class EnterpriseAdmin(MyCompareVersionAdmin):

Related modifications in `AttachmentForm`:

{{{#!python
user = None

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:3>

Django

unread,
Sep 19, 2016, 1:07:06 AM9/19/16
to django-...@googlegroups.com
#27240: Docs - Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by arogachev:

Old description:

> I have `Enterprise` model and related model `Attachment`. Attachments are


> managed using formsets in both frontend and backend.
>
> I use custom form to handle attachments, and it uses additional `user`
> kwarg:
>
> {{{#!python
> def __init__(self, *args, **kwargs):
> self.user = kwargs.pop('user', None)
> super(AttachmentForm, self).__init__(*args, **kwargs)
> }}}
>
> I need to fill this parameter with `request.user` value. In frontend I
> use the method recommended in
> [https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

> custom-parameters-to-formset-forms docs] (code omitted for brevity).


>
> {{{#!python
> enterprise = Enterprise()
> AttachmentFormSet = generic_inlineformset_factory(Attachment,
> form=AttachmentForm, max_num=3, validate_max=True)
> if request.method == 'POST':

> attachment_formset = AttachmentFormSet(request.POST, request.FILES,
> instance=enterprise, form_kwargs = {'user': request.user})

> else:


> attachment_formset = AttachmentFormSet(instance=enterprise,
> form_kwargs = {'user': request.user})
> }}}
>

> But docs do not cover how achieve the same thing in backend using Django
> Admin.
>
> In admin I have:
>
> {{{#!python
> class AttachmentInline(GenericTabularInline):
> model = Attachment
> form = AttachmentForm
>

New description:

I have `Enterprise` model and related model `Attachment`. Attachments are
managed using formsets in both frontend and backend.

I use custom form to handle attachments, and it uses additional `user`
kwarg:

{{{#!python
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

I need to fill this parameter with `request.user` value. In frontend I use
the method recommended in
[https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

custom-parameters-to-formset-forms docs] (code omitted for brevity).

{{{#!python
enterprise = Enterprise()
AttachmentFormSet = generic_inlineformset_factory(Attachment,
form=AttachmentForm, max_num=3, validate_max=True)
if request.method == 'POST':

attachment_formset = AttachmentFormSet(request.POST, request.FILES,
instance=enterprise, form_kwargs = {'user': request.user})

else:


attachment_formset = AttachmentFormSet(instance=enterprise,
form_kwargs = {'user': request.user})
}}}

But docs do not cover how achieve the same thing in backend using Django
Admin.

In admin I have:

{{{#!python
class AttachmentInline(GenericTabularInline):
model = Attachment
form = AttachmentForm

class EnterpriseAdmin(MyCompareVersionAdmin):
inlines = [AttachmentInline]
}}}

It's unclear where and how we need to grab and pass `request.user`


parameter to `AttachmentForm`. After some trial and error, I ended up with
this workaround:

{{{#!python
class EnterpriseAdmin(MyCompareVersionAdmin):
inlines = [AttachmentInline]

def save_related(self, request, form, formsets, change):
AttachmentForm.user = request.user
super(MyCompareVersionAdmin, self).save_related(request, form,
formsets, change)
}}}

Related modifications in `AttachmentForm`:

{{{#!python
user = None

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
if user:
self.user = user
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

So I turned `user` to class attribute and set it from kwargs only if it's
passed and it's not `None`.

It works, but I don't like this approach and It looks like a hack for me.

I think the solution on how to properly do it should be added to the docs
in the same section along with frontend solution.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:4>

Django

unread,
Sep 19, 2016, 1:09:01 AM9/19/16
to django-...@googlegroups.com
#27240: Docs - Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by arogachev:

Old description:

> I have `Enterprise` model and related model `Attachment`. Attachments are


> managed using formsets in both frontend and backend.
>
> I use custom form to handle attachments, and it uses additional `user`
> kwarg:
>
> {{{#!python
> def __init__(self, *args, **kwargs):
> self.user = kwargs.pop('user', None)
> super(AttachmentForm, self).__init__(*args, **kwargs)
> }}}
>
> I need to fill this parameter with `request.user` value. In frontend I
> use the method recommended in
> [https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

> custom-parameters-to-formset-forms docs] (code omitted for brevity).


>
> {{{#!python
> enterprise = Enterprise()
> AttachmentFormSet = generic_inlineformset_factory(Attachment,
> form=AttachmentForm, max_num=3, validate_max=True)
> if request.method == 'POST':

> attachment_formset = AttachmentFormSet(request.POST, request.FILES,
> instance=enterprise, form_kwargs = {'user': request.user})

> else:


> attachment_formset = AttachmentFormSet(instance=enterprise,
> form_kwargs = {'user': request.user})
> }}}
>

> But docs do not cover how achieve the same thing in backend using Django
> Admin.
>
> In admin I have:
>
> {{{#!python
> class AttachmentInline(GenericTabularInline):
> model = Attachment
> form = AttachmentForm
>

> class EnterpriseAdmin(MyCompareVersionAdmin):
> inlines = [AttachmentInline]
> }}}
>

> It's unclear where and how we need to grab and pass `request.user`
> parameter to `AttachmentForm`. After some trial and error, I ended up
> with this workaround:
>
> {{{#!python
> class EnterpriseAdmin(MyCompareVersionAdmin):
> inlines = [AttachmentInline]
>
> def save_related(self, request, form, formsets, change):
> AttachmentForm.user = request.user
> super(MyCompareVersionAdmin, self).save_related(request, form,
> formsets, change)
> }}}
>
> Related modifications in `AttachmentForm`:
>
> {{{#!python
> user = None
>
> def __init__(self, *args, **kwargs):
> user = kwargs.pop('user', None)
> if user:
> self.user = user
> super(AttachmentForm, self).__init__(*args, **kwargs)
> }}}
>
> So I turned `user` to class attribute and set it from kwargs only if it's
> passed and it's not `None`.
>
> It works, but I don't like this approach and It looks like a hack for me.
>
> I think the solution on how to properly do it should be added to the docs
> in the same section along with frontend solution.

New description:

I have `Enterprise` model and related model `Attachment`. Attachments are
managed using formsets in both frontend and backend.

I use custom form to handle attachments, and it uses additional `user`
kwarg:

{{{#!python
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

I need to fill this parameter with `request.user` value. In frontend I use
the method recommended in
[https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

custom-parameters-to-formset-forms docs] (code omitted for brevity).

{{{#!python
enterprise = Enterprise()
AttachmentFormSet = generic_inlineformset_factory(Attachment,
form=AttachmentForm, max_num=3, validate_max=True)
if request.method == 'POST':

attachment_formset = AttachmentFormSet(request.POST, request.FILES,
instance=enterprise, form_kwargs = {'user': request.user})

else:


attachment_formset = AttachmentFormSet(instance=enterprise,
form_kwargs = {'user': request.user})
}}}

But docs do not cover how achieve the same thing in backend using Django
Admin.

In admin I have:

{{{#!python
class AttachmentInline(GenericTabularInline):
model = Attachment
form = AttachmentForm

class EnterpriseAdmin(MyCompareVersionAdmin):
inlines = [AttachmentInline]
}}}

It's unclear where and how we need to grab and pass `request.user`


parameter to `AttachmentForm`. After some trial and error, I ended up with
this workaround:

{{{#!python
class EnterpriseAdmin(admin.ModelAdmin):
inlines = [AttachmentInline]

def save_related(self, request, form, formsets, change):
AttachmentForm.user = request.user

super(EnterpriseAdmin, self).save_related(request, form, formsets,
change)
}}}

Related modifications in `AttachmentForm`:

{{{#!python
user = None

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
if user:
self.user = user
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

So I turned `user` to class attribute and set it from kwargs only if it's
passed and it's not `None`.

It works, but I don't like this approach and It looks like a hack for me.

I think the solution on how to properly do it should be added to the docs
in the same section along with frontend solution.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:5>

Django

unread,
Sep 19, 2016, 1:10:55 AM9/19/16
to django-...@googlegroups.com
#27240: Docs - Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: master

Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by arogachev):

* version: 1.10 => master


--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:6>

Django

unread,
Sep 19, 2016, 5:48:59 AM9/19/16
to django-...@googlegroups.com
#27240: Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody
Type: Uncategorized | Status: new
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by arogachev):

* component: contrib.admin => Documentation


Old description:

> I have `Enterprise` model and related model `Attachment`. Attachments are
> managed using formsets in both frontend and backend.
>
> I use custom form to handle attachments, and it uses additional `user`
> kwarg:
>
> {{{#!python
> def __init__(self, *args, **kwargs):
> self.user = kwargs.pop('user', None)
> super(AttachmentForm, self).__init__(*args, **kwargs)
> }}}
>
> I need to fill this parameter with `request.user` value. In frontend I
> use the method recommended in
> [https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

> custom-parameters-to-formset-forms docs] (code omitted for brevity).


>
> {{{#!python
> enterprise = Enterprise()
> AttachmentFormSet = generic_inlineformset_factory(Attachment,
> form=AttachmentForm, max_num=3, validate_max=True)
> if request.method == 'POST':

> attachment_formset = AttachmentFormSet(request.POST, request.FILES,
> instance=enterprise, form_kwargs = {'user': request.user})

> else:


> attachment_formset = AttachmentFormSet(instance=enterprise,
> form_kwargs = {'user': request.user})
> }}}
>

> But docs do not cover how achieve the same thing in backend using Django
> Admin.
>
> In admin I have:
>
> {{{#!python
> class AttachmentInline(GenericTabularInline):
> model = Attachment
> form = AttachmentForm
>

> class EnterpriseAdmin(MyCompareVersionAdmin):
> inlines = [AttachmentInline]
> }}}
>

> It's unclear where and how we need to grab and pass `request.user`
> parameter to `AttachmentForm`. After some trial and error, I ended up
> with this workaround:
>
> {{{#!python

> class EnterpriseAdmin(admin.ModelAdmin):


> inlines = [AttachmentInline]
>
> def save_related(self, request, form, formsets, change):
> AttachmentForm.user = request.user

> super(EnterpriseAdmin, self).save_related(request, form,


> formsets, change)
> }}}
>
> Related modifications in `AttachmentForm`:
>
> {{{#!python
> user = None
>
> def __init__(self, *args, **kwargs):
> user = kwargs.pop('user', None)
> if user:
> self.user = user
> super(AttachmentForm, self).__init__(*args, **kwargs)
> }}}
>
> So I turned `user` to class attribute and set it from kwargs only if it's
> passed and it's not `None`.
>
> It works, but I don't like this approach and It looks like a hack for me.
>
> I think the solution on how to properly do it should be added to the docs
> in the same section along with frontend solution.

New description:

I have `Enterprise` model and related model `Attachment`. Attachments are
managed using formsets in both frontend and backend.

I use custom form to handle attachments, and it uses additional `user`
kwarg:

{{{#!python
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

I need to fill this parameter with `request.user` value. In frontend I use
the method recommended in
[https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

custom-parameters-to-formset-forms docs] (code omitted for brevity).

{{{#!python
enterprise = Enterprise()
AttachmentFormSet = generic_inlineformset_factory(Attachment,
form=AttachmentForm, max_num=3, validate_max=True)
if request.method == 'POST':

attachment_formset = AttachmentFormSet(request.POST, request.FILES,
instance=enterprise, form_kwargs = {'user': request.user})

else:


attachment_formset = AttachmentFormSet(instance=enterprise,
form_kwargs = {'user': request.user})
}}}

But docs do not cover how achieve the same thing in backend using Django
Admin.

In admin I have:

{{{#!python
class AttachmentInline(GenericTabularInline):
model = Attachment
form = AttachmentForm

class EnterpriseAdmin(MyCompareVersionAdmin):
inlines = [AttachmentInline]
}}}

It's unclear where and how we need to grab and pass `request.user`


parameter to `AttachmentForm`. After some trial and error, I ended up with
this workaround:

{{{#!python
class EnterpriseAdmin(admin.ModelAdmin):
inlines = [AttachmentInline]

def save_related(self, request, form, formsets, change):
AttachmentForm.user = request.user

super(EnterpriseAdmin, self).save_related(request, form, formsets,
change)
}}}

Related modifications in `AttachmentForm`:

{{{#!python
user = None

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
if user:
self.user = user
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

So I turned `user` to class attribute and set it from kwargs only if it's
passed and it's not `None`.

It works, but I don't like this approach and It looks like a hack for me.

I think the solution on how to properly do it should be added to the docs
in the same section along with frontend solution.

I'm not sure If I can't find the proper way to do that or it's really not
implemented for admin.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:7>

Django

unread,
Sep 19, 2016, 5:49:44 AM9/19/16
to django-...@googlegroups.com
#27240: Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by arogachev:

Old description:

> I have `Enterprise` model and related model `Attachment`. Attachments are


> managed using formsets in both frontend and backend.
>
> I use custom form to handle attachments, and it uses additional `user`
> kwarg:
>
> {{{#!python
> def __init__(self, *args, **kwargs):
> self.user = kwargs.pop('user', None)
> super(AttachmentForm, self).__init__(*args, **kwargs)
> }}}
>
> I need to fill this parameter with `request.user` value. In frontend I
> use the method recommended in
> [https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

> custom-parameters-to-formset-forms docs] (code omitted for brevity).


>
> {{{#!python
> enterprise = Enterprise()
> AttachmentFormSet = generic_inlineformset_factory(Attachment,
> form=AttachmentForm, max_num=3, validate_max=True)
> if request.method == 'POST':

> attachment_formset = AttachmentFormSet(request.POST, request.FILES,
> instance=enterprise, form_kwargs = {'user': request.user})

> else:


> attachment_formset = AttachmentFormSet(instance=enterprise,
> form_kwargs = {'user': request.user})
> }}}
>

> But docs do not cover how achieve the same thing in backend using Django
> Admin.
>
> In admin I have:
>
> {{{#!python
> class AttachmentInline(GenericTabularInline):
> model = Attachment
> form = AttachmentForm
>

> class EnterpriseAdmin(MyCompareVersionAdmin):
> inlines = [AttachmentInline]
> }}}
>

> It's unclear where and how we need to grab and pass `request.user`
> parameter to `AttachmentForm`. After some trial and error, I ended up
> with this workaround:
>
> {{{#!python

> class EnterpriseAdmin(admin.ModelAdmin):


> inlines = [AttachmentInline]
>
> def save_related(self, request, form, formsets, change):
> AttachmentForm.user = request.user

> super(EnterpriseAdmin, self).save_related(request, form,


> formsets, change)
> }}}
>
> Related modifications in `AttachmentForm`:
>
> {{{#!python
> user = None
>
> def __init__(self, *args, **kwargs):
> user = kwargs.pop('user', None)
> if user:
> self.user = user
> super(AttachmentForm, self).__init__(*args, **kwargs)
> }}}
>
> So I turned `user` to class attribute and set it from kwargs only if it's
> passed and it's not `None`.
>
> It works, but I don't like this approach and It looks like a hack for me.
>
> I think the solution on how to properly do it should be added to the docs
> in the same section along with frontend solution.
>

> I'm not sure If I can't find the proper way to do that or it's really not
> implemented for admin.

New description:

I have `Enterprise` model and related model `Attachment`. Attachments are
managed using formsets in both frontend and backend.

I use custom form to handle attachments, and it uses additional `user`
kwarg:

{{{#!python
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

I need to fill this parameter with `request.user` value. In frontend I use
the method recommended in
[https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-

custom-parameters-to-formset-forms docs] (code omitted for brevity).

{{{#!python
enterprise = Enterprise()
AttachmentFormSet = generic_inlineformset_factory(Attachment,
form=AttachmentForm, max_num=3, validate_max=True)
if request.method == 'POST':

attachment_formset = AttachmentFormSet(request.POST, request.FILES,
instance=enterprise, form_kwargs = {'user': request.user})

else:


attachment_formset = AttachmentFormSet(instance=enterprise,
form_kwargs = {'user': request.user})
}}}

But docs do not cover how achieve the same thing in backend using Django
Admin.

In admin I have:

{{{#!python
class AttachmentInline(GenericTabularInline):
model = Attachment
form = AttachmentForm

class EnterpriseAdmin(MyCompareVersionAdmin):
inlines = [AttachmentInline]
}}}

It's unclear where and how we need to grab and pass `request.user`


parameter to `AttachmentForm`. After some trial and error, I ended up with
this workaround:

{{{#!python
class EnterpriseAdmin(admin.ModelAdmin):
inlines = [AttachmentInline]

def save_related(self, request, form, formsets, change):
AttachmentForm.user = request.user

super(EnterpriseAdmin, self).save_related(request, form, formsets,
change)
}}}

Related modifications in `AttachmentForm`:

{{{#!python
user = None

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
if user:
self.user = user
super(AttachmentForm, self).__init__(*args, **kwargs)
}}}

So I turned `user` to class attribute and set it from kwargs only if it's
passed and it's not `None`.

It works, but I don't like this approach and It looks like a hack for me.

I think the solution on how to properly do it should be added to the docs
in the same section along with frontend solution.

I'm not sure If I can't find the proper way to do that or it's really not
implemented for admin.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:8>

Django

unread,
Sep 19, 2016, 5:51:48 AM9/19/16
to django-...@googlegroups.com
#27240: Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by knbk):

I'm currently using the following pattern to solve the same problem with
forms:

{{{
def get_form(self, request, obj=None, **kwargs):
Form = super().get_form(request, obj=None, **kwargs)
return functools.partial(Form, user=request.user)
}}}

Using a class attribute can suffer from thread-safety issues when handling
concurrent requests.

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:9>

Django

unread,
Sep 19, 2016, 6:04:55 AM9/19/16
to django-...@googlegroups.com
#27240: Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by arogachev):

@knbk I understand that approach with class attribute is not elegant. As
for your soultion - I need to do it for formset form (`AttachmentForm`),
not admin form. Admin form is different in my case and called
`EnterpriseAdminForm`. Formset has no `get_form` related method, only
`get_form_kwargs(self, index):` method. which has no reference to
`request`.

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:10>

Django

unread,
Sep 19, 2016, 6:17:43 AM9/19/16
to django-...@googlegroups.com
#27240: Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by knbk):

`InlineModelAdmin` has a `get_formset()` method in which you can do the
same.

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:11>

Django

unread,
Sep 19, 2016, 6:22:04 AM9/19/16
to django-...@googlegroups.com
#27240: Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by arogachev):

@knbk Thanks, I think this can be also done in `def
get_formsets_with_inlines(self, request, obj=None):` or `def
get_inline_formsets(self, request, formsets, inline_instances, obj=None):`
in regular `admin.ModelAdmin`.

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:12>

Django

unread,
Sep 20, 2016, 2:42:53 PM9/20/16
to django-...@googlegroups.com
#27240: Passing custom parameters to formset forms in admin
-------------------------------------+-------------------------------------
Reporter: Alexey Rogachev | Owner: nobody

Type: Uncategorized | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

Did you come up with a solution? If so, maybe you can submit a patch if
you still think an example is needed?

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:13>

Django

unread,
Sep 22, 2016, 12:59:25 PM9/22/16
to django-...@googlegroups.com
#27240: Allow passing custom parameters to formset forms in admin

-------------------------------------+-------------------------------------
Reporter: Alexey Rogachev | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: duplicate

Keywords: admin, form, | Triage Stage:
formset, parameter | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => duplicate
* type: Uncategorized => New feature
* component: Documentation => contrib.admin


Comment:

Closing as a duplicate of #26607 (see :ticket:26607#comment:3).

--
Ticket URL: <https://code.djangoproject.com/ticket/27240#comment:14>

Reply all
Reply to author
Forward
0 new messages