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 serge_spaolonzi
- Needs documentation unset
- Needs tests unset
- Owner changed from nobody to serge_spaolonzi
- Patch needs improvement unset
- Status changed from new to assigned
comment:2 Changed 4 years ago by serge_spaolonzi
- Summary changed from Add a new field option for localization in Model Fields. to Add a localization field option in Model Fields.
comment:4 Changed 4 years ago by apollo13
- Cc florian+django@… added
- Triage Stage changed from Unreviewed to Design decision needed
comment:9 Changed 4 years ago by jezdez
- Resolution set to wontfix
- Status changed from assigned to closed