#36498: Inline button to add first / only related instance should say "Add ...",
not "Add another ..."
-------------------------------------+-------------------------------------
Reporter: Denis Washington | Owner: Dorian
Type: | Adams
Cleanup/optimization | Status: assigned
Component: contrib.admin | Version: 5.2
Severity: Normal | Resolution:
Keywords: inline | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 1
-------------------------------------+-------------------------------------
Comment (by Antoliny):
Replying to [comment:4 Dorian Adams]:
> Should the "add" label be based on the actual database contents or on
the user’s visible context? Specifically, when a user has add permission
but lacks change permission on an inline model, existing instances may
exist but won't be visible to the user.
>
> (Is it ok to pose this question here)
I believe it should be possible to base it on the actual database contents
without requiring additional queries! (Of course, I could be wrong.)
In the Django admin inline, the text for adding an inline form is
determined by the inline_formset_data method of the InlineAdminFormSet
class.
And if you look at the `__iter__` magic method, you'll see that the query
is already executed there.(To populate the objects linked to the
InlineForm)
so I think we can simply reuse the result of that query like this...
{{{#!diff
diff --git a/django/contrib/admin/helpers.py
b/django/contrib/admin/helpers.py
index 969167f0e2..8586c20a1f 100644
--- a/django/contrib/admin/helpers.py
+++ b/django/contrib/admin/helpers.py
@@ -419,6 +419,7 @@ class InlineAdminFormSet:
def inline_formset_data(self):
verbose_name = self.opts.verbose_name
+ has_object = any(inlineadminform.original is not None for
inlineadminform in self)
return json.dumps(
{
"name": "#%s" % self.formset.prefix,
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/36498#comment:5>