Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#18506 closed Uncategorized (duplicate)

(Model)Forms localize=True violates DRY

Reported by: Klaas van Schelven Owned by: nobody
Component: Uncategorized Version: 1.4
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


As it is now one cannot make localized ModelForms without violating DRY. Developers w/o the desire for localized data in their application can simply take a model and then create a modelform in three lines and an admin interface in even less. However, a developer that wants localization to work in the admin/their modelforms needs to set the attribute localize=True for each single field that they want localized. This breaks DRY as there is both repetition of the "localize=True" and it also introduces new repetition between models & their forms.

I understand that the decision to make localize=False the default was both deliberate and probably the correct one, as Russel explains here:

However, a couple of notes:

  • I think the case Russel mentions is the exception, not the rule. "Usually" a number is just a number, not a year. Postal codes and phone numbers are often not numbers at all.
  • We (Django) have made the opposite decision for template rendering: Indicating USE_L10N in settings makes localized rendering the default throughout the application, and the developer is left to find the exceptions and note them whenever they occur. This is inconsistent with the way we deal with forms (and has the usual drawbacks of inconsistency, such as the confusion of the OP in the email above).

My suggested solution would be to add an attribute to ModelFields.

We already have a number of cases in which attributes of models' fields are propagated to your modelforms. blank => not required, verbose_name => label and help_text => help_text spring to mind.

I'd say name this attribute "localize" and default it to True (since that's the most common case). But this will introduce backwards breakage, so I can also imagine simply keeping it False as a default. That would at least get rid of the violation of DRY mentioned above. Alternatively the default value of this attribute could be settings.USE_L10N.

Once we have localize=True/False on modelfields, we could even envision using that value in template output as well, i.e. years will automatically be recognized as such and we no longer need to explicitly mark them as non-localized each time they're used in a template. But maybe that's ground for another ticket.

Change History (2)

comment:1 Changed 4 years ago by anonymous

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: duplicate
Status: newclosed

Isn't this #18471?

comment:2 Changed 4 years ago by Klaas van Schelven

Yes, correct, I missed that one.

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