Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28340 closed Cleanup/optimization (wontfix)

USE_THOUSAND_SEPARATOR=True causes primary key to include commas

Reported by: Zach Borboa Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When USE_THOUSAND_SEPARATOR=True, an object's primary key (pk) and identifier (id) fields unexpectedly include commas when used in templates.

Example:
Say I include <input name="article_id" value="{{ obj.pk }}"> in a template with a submit button to save an article, saves will eventually fail QuerySet.get(pk='1,000') containing a comma. Using {{ object.pk }} in a template will initially work, but later fail when the 1000th entry is saved.

Possible solution: Display numbers using a thousand separator when USE_THOUSAND_SEPARATOR=True, except for an object's pk and object's id fields as these are understood to be keys and identifiers, respectively.

Change History (2)

comment:1 by Tim Graham, 7 years ago

Component: UncategorizedTemplate system
Resolution: wontfix
Status: newclosed
Type: UncategorizedCleanup/optimization

Use the unlocalize template filter, e.g. {% load l10n %} {{ obj.pk|unlocalize }}. I don't think some automatic behavior would be desirable considering there could be cases where you want a localized value.

comment:2 by Zach Borboa, 7 years ago

Can this behavior be documented with a note?

    diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt
    index 70402b1fc4..c5d83c38a8 100644
    --- a/docs/ref/settings.txt
    +++ b/docs/ref/settings.txt
    @@ -2669,6 +2669,12 @@ format, it will have higher precedence and will be applied instead.
    See also :setting:`DECIMAL_SEPARATOR`, :setting:`NUMBER_GROUPING` and
    :setting:`THOUSAND_SEPARATOR`.
    
    +.. note::
    +
    +    Using ``{{ object.pk }}`` and ``{{ object.id }}`` may include
    +    :setting:`THOUSAND_SEPARATOR`. Consider using {{ object.pk|unlocalize }}
    +    instead.
    +
    .. setting:: USE_TZ
    
    ``USE_TZ``
Note: See TracTickets for help on using tickets.
Back to Top