Django

Code

Ticket #4435 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

[unicode] get_FIELD_display() does not work for None values

Reported by: mir@noris.de Assigned to: mtredinnick
Milestone: Component: Database layer (models, ORM)
Version: other branch Keywords:
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

If you have a model field with choices and null=True, the model function get_FIELD_display() returns u'None' if the attribute is None. It should return None

django.db.models.base, lines ~ 322 ff:

    def _get_FIELD_display(self, field):
        value = getattr(self, field.attname)
        return force_unicode(dict(field.choices).get(value, value))

Either this function, or force_unicode, needs a special check for None.

Attachments

Change History

05/30/07 11:10:58 changed by mir@noris.de

  • needs_better_patch changed.
  • version changed from SVN to other branch.
  • needs_tests changed.
  • needs_docs changed.

05/31/07 03:33:32 changed by mtredinnick

  • owner changed from adrian to mtredinnick.
  • stage changed from Unreviewed to Accepted.

We can't make force_unicode() do this by default, because it has to behave like str() in a bunch of places. I'll add a parameter to ignore such non-strings, though, just like smart_str() currently has. Then we can nail them one-by-one (there shouldn't be too many).

05/31/07 03:59:53 changed by mtredinnick

Change of mind to the previous comment. I cannot see anywhere at all that we are relying on mapping None -> 'None' via force_unicode. So special-casing None seems reasonable.

05/31/07 04:03:30 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [5388]) unicode: Changed handling of None in smart_unicode/force_unicode. There is no case when converting it to a unicode string seems useful, so keep it as None. Fixed #4435.

06/01/07 02:30:41 changed by mtredinnick

(In [5400]) unicode: Reverted [5388] and fixed the problem in a different way. Checked every occurrence of smart_unicode() and force_unicode() that was not previously a str() call, so hopefully the problems will not reoccur. Fixed #4447. Refs #4435, #4430.


Add/Change #4435 ([unicode] get_FIELD_display() does not work for None values)




Change Properties
Action