Opened 6 years ago

Closed 17 months ago

#26607 closed New feature (fixed)

Add a hook to customize the admin's formsets parameters

Reported by: David Sanders Owned by: Manav Agarwal
Component: contrib.admin Version: dev
Severity: Normal Keywords: admin inline formset initial
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

New feature that adds a method on InlineModelAdmin for providing initial data for the inline formset. By default there is no implementation, although one could be implemented to use GET parameters like get_changeform_initial_data, but it wouldn't be trivial due to the list nature of formset initial data.

Change History (16)

comment:1 Changed 6 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 6 years ago by Philip James

Patch needs improvement: set

Currently the PR has merge conflicts

comment:3 Changed 6 years ago by Tim Graham

Summary: Add InlineModelAdmin.get_formset_initial_data().Add a hook to customize the admin's formsets parameters

I think we should add a more general customization hook that allows customizing the parameters passed to the formset initialization (which includes initial data). That could also allow the use case of #27240 which requires adding form_kwargs': {'request': request} to formset_params.

comment:4 Changed 20 months ago by Manav Agarwal

Owner: nobody deleted
Status: newassigned

comment:5 Changed 20 months ago by Manav Agarwal

Owner: set to Manav Agarwal

comment:6 Changed 20 months ago by Manav Agarwal

consider the model and admin as defined below.
models.py

class Author(models.Model):
    name = models.CharField(max_length=100)


class Book(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)

admin.py

class BookInline(admin.StackedInline):
    model = Book
class AuthorAdmin(admin.ModelAdmin):
    inlines = [
        BookInline,
    ]

admin.site.register(Author, AuthorAdmin)

Is it a good idea to pass the initial vales of bookinline fields by using get request in such a way like

http://127.0.0.1:8000/admin/polls/author/add/?name=Author_name&book_1_name=book1_name_value&book_2_name=book2_name_value

Example:

http://127.0.0.1:8000/admin/polls/author/add/?name=william_shakespeare&book_1_name=Hamlet&book_2_name=Romeo_and_Juliet

Please update me if the idea seems fine so that I may create a PR in order to solve the issue.

Last edited 20 months ago by Manav Agarwal (previous) (diff)

comment:7 Changed 20 months ago by Manav Agarwal

Needs documentation: set
Needs tests: set
Patch needs improvement: unset

comment:8 Changed 19 months ago by David Smith

Patch needs improvement: set

comment:9 Changed 19 months ago by Manav Agarwal

Needs documentation: unset
Needs tests: unset

comment:10 Changed 17 months ago by Manav Agarwal

Patch needs improvement: unset

comment:11 Changed 17 months ago by Mariusz Felisiak

Needs documentation: set

comment:12 Changed 17 months ago by Manav Agarwal

Needs documentation: unset

comment:13 Changed 17 months ago by Mariusz Felisiak

Patch needs improvement: set

Per Nick's comments.

comment:14 Changed 17 months ago by Manav Agarwal

Patch needs improvement: unset

comment:15 Changed 17 months ago by Mariusz Felisiak

Triage Stage: AcceptedReady for checkin

comment:16 Changed 17 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 3119a6d:

Fixed #26607 -- Allowed customizing formset kwargs with ModelAdmin.get_formset_kwargs().

Thanks Nick Pope for reviews.

Note: See TracTickets for help on using tickets.
Back to Top