-
Notifications
You must be signed in to change notification settings - Fork 17
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
Feature Request: Table of unit_choices
conversions within Form Widget
#38
Comments
Dear jacklinke, I agree with your analysis and the idea sounds awesome but also a bit tricky because you would need to link the chosen units with new database fields. I also don't yet fully understand what your definition of "table" is? class Beer:
volume = QuantityField(...)
volume_unit = models.CharField()+
voulme_original = models.Float() Or do you mean to use an Relation like suggested here: |
I was thinking something even simpler than that - an actual html table of the conversions to each unit in Users of (I'm also intrigued by the concept you mention above and will think through how that might also be approached for as a future initiative) |
Ah okay. I understand now. Looks good and simple. But then the naming is a bit confusing. Maybe call the argument I happy to look into your merge request. |
Great suggestions! I'll finish this up and get a PR in this week :) |
If you need any help, let me know. |
@jacklinke, are you still expecting to submit a PR for this? It would be a very nice addition to the library :) |
@mikeford3 It had slipped off my radar a bit, but yes will work o finishing it up this week. |
Some more links to this approach: |
Thanks @jacklinke! I interpreted table as just an HTML table, rather than a table in a database. I was hoping to use this display a small table, like the one Jack showed above, when the user hovers over a field. |
@mikeford3 & @CarliJoy I think this issue should focus on the displayed table, with the changes to the db as a separate issue. With that in mind, I'm nearly done with the work to display a table. Code still needs some tweaks and cleanup, but here's a screenshot I took just now using the dummyapp, using: weight = QuantityFormField(
base_units="gram",
unit_choices=["ounce", "gram", "pound", "ton", "kilogram", "milligram"],
show_conversion=DisplayOption.TABLE,
) The html template for the widget looks like this {% spaceless %}{% for widget in widget.subwidgets %}{% include widget.template_name %}{% endfor %}{% endspaceless %}
{% if values_list %}
<table style="margin:20px;">
<tr>
<th>Unit</th>
<th>Value</th>
</tr>
{% for value_item in values_list %}
<tr>
<td>{{ value_item.units }}</td>
<td style="text-align:right">
{{ value_item.magnitude|floatformat:6 }}
</td>
</tr>
{% endfor %}
</table>
{% else %}
<br>
{% endif %} |
Looks nice. I agree that the DB issue is just something very different. |
Hi @jacklinke, is still this something that you're working on? |
I, too, am looking at enhancing the QuantityField, where the selected magnitude is actually stored with the data. Except, the ideas I've had would render the DB value near useless for any external access/reporting tools. The easiest is to pickle the object. Or, more flexible, converting the Long field to a CharField and appending the magnitude to the value. You would obviously lose the precision characteristics of a Long, but gain descriptive value. i.e. base_unit = minutes, selected = 8 days = "11520:days" |
"Pickling" numbers as strings in a database is not a good idea. As long https://code.djangoproject.com/ticket/5929 is not resolved I don't see a proper way to solve this issue properly. |
I agree about Pickling. It's an idea, but a bad one. Until we can do this "proper", I've written a custom field that just uses markup in the DB. Horrible for external tools to the DB, but it works for me. Looking forward to solving this without markup. i.e. with base_units = 'kilograms', saving an object using 'pounds'
|
Because the value of an item using QuantityFormField defaults to the base unit when updating, even if the user originally entered a value using some other unit from
unit_choices
, they may later have some confusion about what value they originally entered.To alleviate this, providing an optional table with converted values from the base unit to each of the unit choices within the form widget would be helpful. For configuration, it should only require something like setting
show_table=True
in the form field definition of a Django Form, defaulting toFalse
if the attribute is not provided.In cases where end users may set a value in a form and then need to edit that value in the future, this would help them to identify the current value since they can see both the base unit (in the input field itself) and their preferred unit (as well as any other unit choices) in the table below the input.
Additionally, users of
django-pint
should be able to override the template used for this table within the widget to meet their needs.If all of this sounds like something that would be beneficial, please let me know and I'll submit a PR to provide the functionality and update the readme.
The text was updated successfully, but these errors were encountered: