Thus, if you have a CharField with a value of `''` (empty string), the
`empty_value_display` value will not appear, contrary to the
documentation.
How to reproduce:
- Create a model with a `CharField(blank=True, null=True)`
- Create a model_admin and add that field to the `list_display` list
- Create two instances of the model, one with that field set to None and
another with it set to `''` (empty string)
- Open the admin panel and find those models, you will see that the
instance with None shows `-` but the one with the empty string shows
nothing.
--
Ticket URL: <https://code.djangoproject.com/ticket/28404>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "empty_string bug.png" added.
Empty strings with no empty_value
* easy: 1 => 0
Comment:
I'm not immediately convinced that the behavior should be changed instead
of correcting the documentation to reflect the current behavior. Has the
behavior changed since the documentation was introduced in
0207bdd2d4157c542c981264c86706b78ca246e9 or was the original documentation
inaccurate?
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:1>
Comment (by Mark Koh):
The functionality hasn't changed since the documentation was updated, so
it seems as though the original documentation was inaccurate. While I do
have a [https://github.com/django/django/pull/8776 PR in for the fix
(WIP)], I agree that it may make more sense to simply correct the
documentation.
On the other hand, I do feel that this functionality would be useful as an
additional overridable property for the ModelAdmin. Perhaps as an
optional `empty_string_display` property?
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:2>
Comment (by Tim Graham):
I've grown wearisome of all the `ModelAdmin` hooks and complexity that
they add. That's the only grounds on which I'd object to an additional
property. If you have a compelling use case, I guess it's okay. I guess
you're finding `empty_value_display` useful?
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:3>
Comment (by Mark Koh):
I would definitely find it useful if it worked with empty strings as well
as `None`. Currently we have a field in our ModelAdmin that is a
`CharField` with `blank=True` and `null=False` and that field is also
specified in the `list_display_links`. Thus, when that field value is an
empty string we are unable to click it (you can see this in the image
attached to this ticket).
Seeing as the `blank=True, null=False` method is the recommended method
for supporting nullable CharFields I imagine that I'm not the only person
whose seen this problem. I am open to either allowing
`empty_value_display` to acknowledge empty strings or to creating a new
optional attribute for empty strings.
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:4>
* stage: Unreviewed => Accepted
Comment:
I don't know if adding a separate property will be simpler than modifying
the existing `empty_value_display`. I guess there could be use cases for a
separate value for each case. You could write to the DevelopersMailingList
for further feedback.
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:5>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:6>
* owner: nobody => Nazarov Georgiy
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:7>
* needs_better_patch: 1 => 0
* version: 1.11 => master
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:8>
* stage: Ready for checkin => Accepted
Comment:
Hi, Nazarov Georgiy, you can't check "Ready for checkin" yourself, please
see [https://docs.djangoproject.com/en/1.11/internals/contributing
/triaging-tickets/ triaging-tickets]
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:9>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:10>
Comment (by Carlton Gibson):
I haven't yet seen a reason not to re-use `empty_value_display` here
(rather than add a new admin option) but we need to consider when that is
applied:
> There are some cases where I really do want an empty string displayed
(though not in a linked column). Maybe only use it only when it's an auto-
linked field/column? Or maybe only use this behavior in
display_for_field()? (So if you use a custom function/method, you can
still have full control.) — [https://groups.google.com/d/topic/django-
developers/6HbmI3rHdPc/discussion Collin Anderson on Django Dev]
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:11>
* cc: Sardorbek Imomaliev (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:12>
Comment (by fgebala):
Confirming that this is definitely an issue with Django 1.11, and the docs
for 1.11 still say this works for empty strings, which it does not:
https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.empty_value_display.
These docs should be updated to avoid confusion in the future.
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:13>
Comment (by Dominik):
Are there any updates on this?
I just stumbled on this issue when trying to use a "nullable" char field
(blank=True, null=False as the docs suggest) in a changelist filter. The
filter option for the empty string is displayed as a literal empty string,
which results in the surrounding link element collapsing to height 0 and
being virtually unusable since you can't click it. The link itself, once
accessed via inspector works fine.
Docs are also unchanged, so what is the verdict on this issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:14>
Comment (by Simon Charette):
Given the `db.models.Field` has
[https://github.com/django/django/blob/62ffc9883afdc0a9f9674702661062508230d7bf/django/db/models/fields/__init__.py#L120
an API that defines what empty values should be] I think that admin logic
should be changed to rely on it?
{{{#!python
diff --git a/django/contrib/admin/templatetags/admin_list.py
b/django/contrib/admin/templatetags/admin_list.py
index 5865843dce..a570026b1e 100644
--- a/django/contrib/admin/templatetags/admin_list.py
+++ b/django/contrib/admin/templatetags/admin_list.py
@@ -227,7 +227,7 @@ def link_in_col(is_first, field_name, cl):
else:
if isinstance(f.remote_field, models.ManyToOneRel):
field_val = getattr(result, f.name)
- if field_val is None:
+ if field_val in f.empty_values:
result_repr = empty_value_display
else:
result_repr = field_va
}}}
That would at least make the notion of ''empty'' coherent between models
and the admin. It kind of blurs the line it terms of what change list
filtering by empty value mean when there's multiple candidates though
(e.g. `CharField(blank=True, null=True)`). Kind of wish we had an
`__empty` lookup for this purpose that would be an alias for all allowed
empty values by the field definition:
{{{#!python
class EmptyModel(models.Model):
text = TextField(blank=True, null=False)
nullable_text = TextField(blank=True, null=True)
integer = IntegerField(blank=True, null=True)
array = ArrayField(TextField(), null=True)
json = JSONField()
EmptyModel.objects.filter(
text__empty=True, # Q(text="")
nullable_text__empty=True, # Q(nullable_text="") |
Q(nullable_text=None)
integer__empty=True, # Q(integer=None)
array__empty=True, # Q(array=[]) | Q(array=None)
json__empty=True, # Q(json=[]) | Q(json={}) | Q(json=None)
)
}}}
If this existed the admin filter could for empty value could basically be
`__empty=True`
[https://github.com/django/django/blob/7119f40c9881666b6f9b5cf7df09ee1d21cc8344/django/contrib/admin/filters.py#L247
instead of] `__isnull=True`.
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:15>
* cc: Sarah Boyce (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:16>
* owner: Nazarov Georgiy => Alexander Lazarević
Comment:
After watching how to perform the "vulture method" in (1) I found this
ticket.
According to “Claiming” tickets' I reassign this ticket to myself,
because "... If a ticket has been assigned for weeks or months without any
activity, it’s probably safe to reassign it to yourself." I hope that's
ok.
(1) https://fosstodon.org/@djang...@indieweb.social/111697455678403023
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:17>
Comment (by Sarah Boyce):
Yes that's completely fine - enjoy!
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:18>
Comment (by Alexander Lazarević):
New PR https://github.com/django/django/pull/17699
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:19>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"1b0a8991aec7079405be6b4be5fb17d69e1f4ab9" 1b0a899]:
{{{
#!CommitTicketReference repository=""
revision="1b0a8991aec7079405be6b4be5fb17d69e1f4ab9"
Refs #28404 -- Split test_null_display_for_field() test.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:20>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:21>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"9b02ad91ead3db75036be981bab2083aebc993a6" 9b02ad9]:
{{{
#!CommitTicketReference repository=""
revision="9b02ad91ead3db75036be981bab2083aebc993a6"
Fixed #28404 -- Made displaying values in admin respect Field's
empty_values.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:22>
Comment (by Alexander Lazarević):
Yeah! Thanks for your support, Mariusz!
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:23>
Comment (by Alexander Lazarević):
I'm afraid my PR might not have been complete. I just tried something like
this:
{{{
@admin.register(SomeModel)
class SomeModelAdmin(admin.ModelAdmin):
list_display = (
"name",
"description",
"my_description",
)
empty_value_display = "-empty-"
@admin.display(empty_value="-very empty-")
def my_description(self, obj):
return "" # obj.description
}}}
and it did not show the "-very empty-" string.
I guess this needs to be changed as well:
{{{
def display_for_value(value, empty_value_display, boolean=False):
from django.contrib.admin.templatetags.admin_list import _boolean_icon
if boolean:
return _boolean_icon(value)
elif value is None: <-------------------------
return empty_value_display
elif isinstance(value, bool):
return str(value)
...
}}}
Can somebody please confirm this? Sorry for not seeing it earlier ...
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:24>
* status: closed => new
* has_patch: 1 => 0
* resolution: fixed =>
* stage: Ready for checkin => Accepted
Comment:
Agreed, let's reopen.
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:25>
* has_patch: 0 => 1
Comment:
Additional PR https://github.com/django/django/pull/17720
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:26>
* status: new => assigned
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:27>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"4787972c941b0d090cf083e84a98c1791bb2ae4b" 4787972]:
{{{
#!CommitTicketReference repository=""
revision="4787972c941b0d090cf083e84a98c1791bb2ae4b"
Refs #28404 -- Made displaying property values in admin respect non-None
empty values.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:28>
* status: assigned => closed
* resolution: => fixed
--
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:29>