Hi.

You could create custom form field that makes conversions both ways.

ke 8. toukok. 2019 klo 1.25 Tim Bell <timothyb...@gmail.com> kirjoitti:

> Hi,
>
> I have a model with an integer field storing a duration in hours, while I
> want to present that to users in a form in days. (The choice of these
> different units is imposed by other factors not under my control.)
>
> So, when saving a form, I need to convert a value in days to hours by
> multiplying by 24. I've done that with a clean_<fieldname>() method on
> the form, as described at
> https://docs.djangoproject.com/en/2.2/ref/forms/validation/. That works
> fine for when saving a new model.
>
> When editing an existing model however, I need to take the value from the
> model stored in hours, and convert that to days for display in the edit
> form. I've considered various places where I could do that conversion, for
> instance in the edit view, or the form __init__() method, or the model
> field's value_from_object() method, but none of those choices seem like
> the obvious choice, and I can't find a recommendation for how to do this
> either.
>
> What would you suggest? And is the recommended approach actually
> documented somewhere that I've missed?
>
> Thanks,
>
> Tim
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/0d786f87-8c6e-4e0d-abc5-aaa66ab4da2a%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/0d786f87-8c6e-4e0d-abc5-aaa66ab4da2a%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHn91od3y53TA%3DYt_x8noAdPYBni8Q4P8mSorJrb2Y%2BD6zcffg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to