bhomnick / django-nonrelated-inlines Goto Github PK
View Code? Open in Web Editor NEWDjango admin inlines for unrelated models
License: MIT License
Django admin inlines for unrelated models
License: MIT License
Hi @bhomnick
I have another problem: the nonrelated inline I define (a Publication) has an m2m field (to Author objects).
I use NRI to show the publications of an author in its change form.
In the save_new_instance
method I explicitly add the parent
object (the current author) to the set of authors:
def save_new_instance(self, parent, instance):
if not instance.id:
instance.save() # otherwise the add below fails
instance.authors.add(parent)
This indeed adds the author to the authors of the new publication (and was intended as a safeguard in case the user forgot to indicate the author as part of the set of authors).
The problem is that the resulting publication only has this author: if the user specifies other authors in the inline form, they are all discarded. Indeed, after save()
above, the set of authors is always empty.
I should say that once the inline instance is created, I can edit the set of authors as expected.
How can I avoid losing the authors when creating a new publication inline?
Thanks!
Hi,
I'm trying to use the ordering
field in a nonrelated inline, but it is not taken into account.
I have project objects, with a year
attribute, and despite putting ordering = ['year']
, the inlines show up in an order that reflects their ids instead of their year attribute.
Any idea?
Thanks!
Interesting module.
Any option to make stackedinline as tabular?
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!
for a better understanding of how to use the application it would be good to create some examples of use
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.