Add a localization field option in Model Fields.
|Reported by:||Serge Spaolonzi||Owned by:||Serge Spaolonzi|
|Component:||Database layer (models, ORM)||Version:||1.4|
|Cc:||florian+django@…||Triage Stage:||Design decision needed|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Add a new field option for localization in Model Fields in order to simplify the process of localization.
It happens that activating the localization of a field in the Admin site from a model is a complex task now.
Instead it can be done easily using only a parameter in the Model declaration, in order to archive this I had to modify three lines in the file django.db.models.fields.init.py
Expected result localization of a decimal field in a model according to the THOUSAND_SEPARATOR setting.
Currently it is done this way
import django class LocalizedModelForm(django.forms.ModelForm): def __new__(cls, *args, **kwargs): new_class = super(LocalizedModelForm, cls).__new__(cls, *args, **kwargs) for field in new_class.base_fields.values(): if isinstance(field, django.forms.DecimalField): field.localize = True field.widget.is_localized = True return new_class class FooForm(LocalizedModelForm): class Meta: model = Foo django.admin.site.register(Foo, form=FooForm)
After improvement it can be done easily
class Foo(models.Model) price = models.DecimalField(verbose_name='Price', max_digits=20, decimal_places=2, localize=True)
I have the patched code in my Github fork, I will update this ticket with the link to my github code.
Change History (10)
comment:1 Changed 4 years ago by
|Owner:||changed from nobody to Serge Spaolonzi|
|Patch needs improvement:||unset|
|Status:||new → assigned|
comment:2 Changed 4 years ago by
|Summary:||Add a new field option for localization in Model Fields. → Add a localization field option in Model Fields.|
comment:4 Changed 4 years ago by
|Triage Stage:||Unreviewed → Design decision needed|