Skip to content

inlines and baton tabs #5

@etanter

Description

@etanter

Hi @bhomnick @abidibo

I recently tried to use nonrelated inlines in order to display stacked inlines for a many-to-many relationship in the Django admin, embellished by Baton.
Basically, there are Person objects and Publication objects, with an M2M relation, and I want to display all the publications (co-)authored by a given Person when viewing a Person's change form. Using .through does not work (as described here), because then it shows the list of relation objects (Person-Publication relations), not the target objects (the Publication objects).

I figured nonrelated inlines could help [*], and they do! It was trivial to define the get_form_queryset and save_new_instance methods and get rolling.

In essence, I have:

class PubInline(NonrelatedStackedInline):
    model = Publication

    def get_form_queryset(self, obj):
        return self.model.objects.filter(authors=obj.id)

    def save_new_instance(self, parent, instance): ...

Now, Baton has a quite flexible (and intricate ;)) way of configuring tabs in admin change forms. One can either include inlines, or custom fieldsets, and package those in tabs as needed. The problem I'm reporting requires understanding the tab mechanism of Baton.

If I use a nonrelated inline (NRI for short), then the baton mechanism to include the inline in a given tab (ie. baton-tab-inline-MODELNAME) does not work: the content of the inline is included (the Publication objects), and the interaction works perfectly (collapsing, adding a new object, etc.), but it always displays the group of inlines on the first tab (which can be moved using the order-n class but that would be a hack that doesn't scale if I had several NRIs to place in different tabs). It's oblivious to where I add the --inline-publications suffix (publications is the related_name to get from a Person to its Publications).
I've also tried --inlines-publication suffix (since Publication is the model underlying PubInline).

Another option that kind of works is to mark the NRI with a class for a fieldset in a tab (not an inline!), basically adding this to PubInline above:

    classes = ['tab-fs-pubs']

Then the placement mechanism works as expected by adding the --fs-pubs suffix wherever I want it to appear.
I say "kind of works" because by proceeding this way, Baton(/Django?) does not recognize the inline as an actual inline, and so the "Add a new publication" button (with its associated JS collapse/edit mechanism) does not show up. So that trick only allows me to edit existing publications, but not create a new one.

My feeling is that the issue here strongly depends on how Baton works under the hood, and making sure that NRI fakes it well enough for Baton. That might require both @bhomnick @abidibo to interact, I'm afraid!

Thanks to both!

[*] maybe there's a Django fix to that M2M/inline/baton problem that doesn't require nonrelated inlines, in which case I'd be happy to hear about it!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions