Opened 9 years ago

Closed 4 years 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 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Philip James, 8 years ago

Patch needs improvement: set

Currently the PR has merge conflicts

comment:3 by Tim Graham, 8 years ago

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 by Manav Agarwal, 4 years ago

Owner: nobody removed
Status: newassigned

comment:5 by Manav Agarwal, 4 years ago

Owner: set to Manav Agarwal

comment:6 by Manav Agarwal, 4 years ago

Considering 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.

Version 0, edited 4 years ago by Manav Agarwal (next)

comment:7 by Manav Agarwal, 4 years ago

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

comment:8 by David Smith, 4 years ago

Patch needs improvement: set

comment:9 by Manav Agarwal, 4 years ago

Needs documentation: unset
Needs tests: unset

comment:10 by Manav Agarwal, 4 years ago

Patch needs improvement: unset

comment:11 by Mariusz Felisiak, 4 years ago

Needs documentation: set

comment:12 by Manav Agarwal, 4 years ago

Needs documentation: unset

comment:13 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set

Per Nick's comments.

comment:14 by Manav Agarwal, 4 years ago

Patch needs improvement: unset

comment:15 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:16 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

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