-
Notifications
You must be signed in to change notification settings - Fork 311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make fixed currency possible #720
Comments
It is possible at least a bit, by defining a default_widget: amount = MoneyField(
currency_widget=Select(choices=[("EUR", _("Euro"))], attrs={"disabled": True}),
) But you have to to this fo each field manually, and it somehow doesn't send the value of the field back to the form, so |
@nerdoc Maybe a project-wide
I think that this behavior could be undesired for projects where the currency choices are dynamic. But if you can configure a currency widget for the entire project, maybe that will work?
This level of customization won't work as a project setting, since the widget is also used in the Django admin which doesn't have Bootstrap. In these cases, it's probably better to find another way to override MoneyField's composite widget. Forms always require a certain amount of customization work. But there are many patterns to achieve reusability across entire sites, without affecting the Django Admin. You could consider working with django-crispy-forms form this. |
Yes, that would be a possibility, because then it can be fully customized per project. Even if customizing a widget is more work than a sensitive setting.
Sure. I didn't mean using BS hard coded in django money ;-) First, I just tried to disable the widget: class SomeForm(forms.ModelForm):
amount = MoneyField(
# currency_widget=Select(choices=[("EUR", _("Euro"))], attrs={"disabled": True}), # <- buggy
currency_widget=Select(choices=[("EUR", _("Euro"))]),
)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.fields["amount"].widget.attrs["autofocus"] = True # works perfectly
self.fields["amount"].widget.widgets[1].attrs["disabled"] = True # <- buggy in these two ways shown above, the second widget gets disabled - which is my desired behaviour if there is only one currency choice. This can be made dynamically too. It works for rendering (and yes, I aready am using crispy forms). The problem here is that I always get a form error when posting - that the whole amount field is required. It seems that if the field is disabled, it does not return a value (here as "amount_1") to the view, which lets the cleaning fail. So easier than creating a custom project wide custom widget implementation would IMHO be allowing users customize the widget. Maybe this could even be thought further and letting the user directly access the widget using I didn't examine the internals of MoneyField that far, but I could not find a cause for letting the value vanish when the field is disabled. Maybe this is Django's normal behaviour, but as the currency is a required field it could be fixed in a Use case:
In my case, it looks like that (for a simple cashbook) - I already implemented the disabled state here: This is what it SHOULD look like. But with above code, it does not work, as stated. Instead, I did a trick and used self.fields["amount"].widget.widgets[1].attrs["hidden"] = True So the currency_field is hidden, but still in the form (hence returning a value and preventing the required constraint). The important thing is that it should not have a tab stop - the cursor should go directly to the next (comment) field when the user presses [TAB] and should not land on the currency field. I know, this all sounds a bit over-engaged - but I like optimizing to death :-) |
Instead of using the |
The select tag in HTML doesn't have a readonly attribute... And HidenInput widged is used if the |
What I have to admit, that this: works, at least of skipping the cursor: amount = MoneyField(
currency_widget=Select(choices=[("EUR", _("Euro"))], attrs={"disabled": True}),
) It's just that
|
There are use cases where only one currency is supported under certain circumstances, e.g. the user in an app has chosen the currency in his settings. I would like to just allow this one currency in the MoneyWidget, but the code does not allow that. What is possible:
MoneyField(default_currency="EUR")
- which only selects EUR as default, but allows all other currencies too.MoneyField(currency_choices=[("EUR", _("Euro"))])
- which reduces the choices to one, but it is still a<select>
.in the MoneyField code,
currency_field
is hard coded:So this does not allow any customization.
What I really want is something like this:
Like a fixed input-group-text, here in e.g. Bootstrap5. This could be made customizable.
For that, It would be enough to add code to MoneyWidget: if there is only one
currency_choices
, it should not be displayed as select - or at least, to be in sync with the form requirements, to disable the select, which has a similar effect, and would be clearer for the user - he is not tempted to click on the select - which has no choices anyway.The text was updated successfully, but these errors were encountered: