Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#30280 closed Bug (fixed)

Model_get_FIELD_display() returns proxies in 2.2rc1 but always returned strings previously

Reported by: Matthias Kestenholz Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

I'm not sure whether that was wanted but cc79c7ee637e65c8da27e56d746c87903d5ec901 (#27795) introduced a change where code which calls get_*_display got a string previously and now gets a proxy under some circumstances.

If this isn't wanted (and I cannot find anything in the release notes saying that it is) then that's a regression which should be fixed before releasing Django 2.2.

Change History (5)

comment:1 by Matthias Kestenholz, 6 years ago

... thinking about this some more, maybe it makes sense to return lazy strings when using lazy strings inside choices.

If that's the case this would certainly warrant a mention in the release notes (or maybe I haven't found it?)

comment:2 by Tim Graham, 6 years ago

Description: modified (diff)
Summary: _get_FIELD_display returns proxies in 2.2rc1 but always returned strings previouslyModel_get_FIELD_display() returns proxies in 2.2rc1 but always returned strings previously
Triage Stage: UnreviewedAccepted

Probably str() could be used where force_text() was used previously. By the way, you should explain how to reproduce the issue, i.e. what "under some circumstances" means.

comment:3 by Matthias Kestenholz, 6 years ago

You're right, thanks. I could have explained it better.

PR

The code which failed was:

class Project(Model):
    ACQUISITION = "acquisition"

    TYPE_CHOICES = [
        (ACQUISITION, _("Acquisition")),
        ...
    ]

    type = models.CharField(_("type"), choices=TYPE_CHOICES, max_length=20)
    closed_on = models.DateField(_("closed on"), blank=True, null=True)

    def pretty_status(self):
        parts = [self.get_type_display()]
        if self.closed_on:
            parts.append(
                gettext("closed on %s") % local_date_format(self.closed_on, "d.m.Y")
            )
        return ", ".join(parts)

comment:4 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: newclosed

In ea071870:

Fixed #30280 -- Restored Model.get_FIELD_display()'s coercion of lazy strings.

Reverted cc79c7ee637e65c8da27e56d746c87903d5ec901.

comment:5 by Tim Graham <timograham@…>, 6 years ago

In a86ffb3e:

[2.2.x] Fixed #30280 -- Restored Model.get_FIELD_display()'s coercion of lazy strings.

Reverted cc79c7ee637e65c8da27e56d746c87903d5ec901.

Backport of ea071870f943c23a8eaf36dfcdf382afd6478fd1 from master.

Note: See TracTickets for help on using tickets.
Back to Top