Let Django users customize FORMAT_SETTINGS (a feature request)

149 views
Skip to first unread message

אורי

unread,
Dec 1, 2019, 11:32:53 PM12/1/19
to Django developers (Contributions to Django itself)
Django developers,

I just added this feature request:

I customized FORMAT_SETTINGS in Speedy Net, you can see more information in this commit:
https://github.com/speedy-net/speedy-net/commit/f5dc4896c136a816ab9fe0dc360d3fd59d0a5f33  

To customize FORMAT_SETTINGS I had to patch 14 files, because the tests failed and I had to patch the tests too. Is it possible to let Django users customize FORMAT_SETTINGS without having to patch files? For example you can check if the settings contain FORMAT_SETTINGS, and if it is - include these settings in the global FORMAT_SETTINGS (a union, like in the above commit).

formats.FORMAT_SETTINGS = formats.FORMAT_SETTINGS.union(django_settings.FORMAT_SETTINGS)

אורי

unread,
Dec 1, 2019, 11:40:24 PM12/1/19
to Django developers (Contributions to Django itself)
Hi,

If you accept this feature request I can try to write it myself as a pull request to Django.

Carlton Gibson

unread,
Dec 2, 2019, 9:03:23 AM12/2/19
to Django developers (Contributions to Django itself)
Hi Uri. 

Can you explain (I couldn't exactly follow from the ticket/commit/SO post) why Custom Format Files[0] 
are not the way to go for you here? 


Thanks. 
Carlton

אורי

unread,
Dec 2, 2019, 10:19:11 AM12/2/19
to Django developers (Contributions to Django itself)
Hi Carlton,

I don't understand how this is related to Custom Format Files.

I want to be able to define settings, such as YEAR_FORMAT which I defined in Speedy Net, where I would define a default value (such as 'Y') which can be overridden for some specific locales. If it's not overridden, the value will come from the global setting, which I define in the settings file of the project. This will expand the list defined in https://github.com/django/django/blob/master/django/utils/formats.py#L31-L46 ('DATE_FORMAT', 'TIME_FORMAT' and so on).


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/22755ead-1fe0-460a-9581-41c920f4e424%40googlegroups.com.

Carlton Gibson

unread,
Dec 2, 2019, 10:35:15 AM12/2/19
to Django developers (Contributions to Django itself)
Hi Uri. 


On Monday, 2 December 2019 16:19:11 UTC+1, Uri wrote:
I don't understand how this is related to Custom Format Files.

Well in the SO post you linked you say this:

> I want to override the default values of DATE_FORMAT and MONTH_DAY_FORMAT in English, but keep the default values (or define them again) in Hebrew.

In the docs I linked you to it says this: 

> Django provides format definitions for many locales, but sometimes you might want to create your own, because a format files doesn’t exist for your locale, or because you want to overwrite some of the values.

So that looks exactly like what you need. 

No? 

(Sorry if I've missed your point — it's sometimes hard to see what folks are getting at just from links and commits. 😬)

Kind Regards,

Carlton

 

Carlton Gibson

unread,
Dec 2, 2019, 10:36:03 AM12/2/19
to Django developers (Contributions to Django itself)
Also, I'm guessing this has come up before, so it's likely others will be able to comment.

אורי

unread,
Dec 2, 2019, 10:58:37 AM12/2/19
to Django developers (Contributions to Django itself)
Hi Carlton,


FORMAT_SETTINGS = frozenset([
    'DECIMAL_SEPARATOR',
    'THOUSAND_SEPARATOR',
    'NUMBER_GROUPING',
    'FIRST_DAY_OF_WEEK',
    'MONTH_DAY_FORMAT',
    'TIME_FORMAT',
    'DATE_FORMAT',
    'DATETIME_FORMAT',
    'SHORT_DATE_FORMAT',
    'SHORT_DATETIME_FORMAT',
    'YEAR_MONTH_FORMAT',
    'DATE_INPUT_FORMATS',
    'TIME_INPUT_FORMATS',
    'DATETIME_INPUT_FORMATS',
])

Is there a way to extend it and add more values?

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.

Carlton Gibson

unread,
Dec 2, 2019, 11:07:51 AM12/2/19
to django-d...@googlegroups.com
Well not currently no. 😀

What I’m trying to ask is why you want to do this? What does you code look like without it? 

Those formats are used by Django. Why would you want/need to put your own values in the same place? 

get_format() returns what you pass it if your format is not found, yes? So is a wrapper function, to fetch your default in that case, not sufficient? 

If not, why not? And even if it is maybe a change Is still in order. Is that the improvement you’re suggesting? 

And so on...

But that’s not the same topic as overriding the original values, for which custom format files does serve I think. 

Again, sorry if I’m not quick enough for you. Just trying to get clear? 

Carlton Gibson

unread,
Dec 2, 2019, 12:42:40 PM12/2/19
to Django developers (Contributions to Django itself)
Hi Uri. 

> To customize FORMAT_SETTINGS I had to patch 14 files, because the tests failed and I had to patch the tests too.

I managed to get a moment to look at your Speedy Net code. The 14 files here is because you have so many entry points (multiple manage.py and wsgi.py files) — most project just don't have that. 

Your locale patches was a pretty simple work-around: 

from django.conf import settings as django_settings
from django.utils import formats

def patch():
   formats.FORMAT_SETTINGS = formats.FORMAT_SETTINGS.union(django_settings.FORMAT_SETTINGS)

I'm wondering if any logic we could add would be worth the complication given that this is available...(?)

On Monday, 2 December 2019 05:32:53 UTC+1, Uri wrote:

אורי

unread,
Dec 2, 2019, 10:27:03 PM12/2/19
to Django developers (Contributions to Django itself)
Hi Carlton,

I want to use YEAR_FORMAT in the template the same why I'm using DATE_FORMAT and MONTH_DAY_FORMAT:

{% if can_view_profile and user.date_of_birth %}
{% if can_view_dob_day_month or can_view_dob_year %}
<tr>
<th nowrap="nowrap">{% if can_view_dob_day_month %}{% trans 'Birth Date' %}{% elif can_view_dob_year %}{% trans 'Birth Year' %}{% endif %}</th>
<td>
{% if can_view_dob_day_month and can_view_dob_year %}
{{ user.date_of_birth|date:"DATE_FORMAT" }}
{% elif can_view_dob_day_month %}
{{ user.date_of_birth|date:"MONTH_DAY_FORMAT" }}
{% elif can_view_dob_year %}
{{ user.date_of_birth|date:"YEAR_FORMAT" }}
{% endif %}
</td>
</tr>
{% endif %}
{% endif %}

And to be able to customize YEAR_FORMAT for specific locales if it needs to be customized. But not to define it if it's equal to the default value which I defined.

Does Custom Format Files do it?

I'm going to try to implement the patch on Speedy Net right now without having to update 14 files. There is a suggestion on Stack Overflow.

אורי


Carlton Gibson

unread,
Dec 4, 2019, 10:31:51 AM12/4/19
to Django developers (Contributions to Django itself)
Hi Uri. 

On Tuesday, 3 December 2019 04:27:03 UTC+1, Uri wrote:
I want to use YEAR_FORMAT in the template the same why I'm using DATE_FORMAT and MONTH_DAY_FORMAT:
And to be able to customize YEAR_FORMAT for specific locales if it needs to be customized. But not to define it if it's equal to the default value which I defined

Right, with you. (Sorry, it takes a while to get clear sometimes.) 

Does Custom Format Files do it?

Yes — all except the default right? — IIUC You can define whatever formats you want in format.py files, and that'll all work how you want 
BUT (unless all your languages were in the same family, en-us, en-gb say, which are both en) it doesn't "fallback" the default locale, 
so you can't set a default. 

With two locales I guess it'd be OK, but with 40... 

So here's my question then (all): With translations we fallback to the default locale. Should it be the case that django.utils.formats.iter_format_modules()
also fallbacks to the default locale? (As it doesn't look like it does currently.) (By appending that to the list of modules to search.)

i.e. is this a bug (or new feature) in format localization that we could patch, within the existing machinery, rather than adding a setting, or needing to go via the global settings file?

(A test case for that new behaviour and a patch, assuming nothing breaks, might swing that, pending lack of responses...)

I'm going to try to implement the patch on Speedy Net right now without having to update 14 files. There is a suggestion on Stack Overflow.

Yes, in the meantime your patch applied in an AppConfig looks my cleaner. :+1: 

Kind Regards,

Carlton

Reply all
Reply to author
Forward
0 new messages