Improve adding data to forms programmatically

110 views
Skip to first unread message

Moritz Sichert

unread,
Sep 15, 2015, 4:23:02 PM9/15/15
to Django developers (Contributions to Django itself)
Hello,

currently you can add data to forms either by passing it into the
"initial" or the "data" argument of Form. However sometimes I have the
need to add data to a Form that isn't request.POST and doesn't come from
an http request.

My use case is the following:

I have a form where a user can edit a model. So I pass the current
values into "initial". Now the user can "save" his current changes
without saving the model instance. He can come back later and see the
changes he already made. So "initial" is still the data from the model
but now "data" has also be filled with the cached changes.
Only when the user clicks on "change model" the changes will actually be
made to the model.

The problem now is that when the user "saves" his intermediate changes I
get the data from form.cleaned_data and cache it somewhere. However if I
want to display the form back to the user I have to convert the data
from the format

{'field1': 'value1', 'field2': 'value2'}

into something like

{'form-prefix-widget-specific-field1': 'value1',
'form-prefix-widget-specific-field2': 'value2'}

and have to actually know how the widgets get their data.

I'd like to propose a new argument to "django.forms.Form" called
"data_values".
Its semantics will be just like data. The difference is that it will be
a dictionary storing the data in {'field-name': 'field-value'} format
instead of a widget specific one.

I have a branch where I sketched this new functionality here:
https://github.com/MoritzS/django/tree/forms-data-values

I'd like to hear your comments on this!

Moritz

signature.asc

Florian Apolloner

unread,
Sep 16, 2015, 7:16:50 AM9/16/15
to Django developers (Contributions to Django itself)
What is preventing you from passing the saved changes in via initial?

Moritz Sichert

unread,
Sep 16, 2015, 7:32:47 AM9/16/15
to django-d...@googlegroups.com
If the user changes a field "username" for example and then saves his
changes form.has_changed() will detect correctly that the username has
changed.
If the user then goes back and changes the username to the old value,
form.has_changed() will return True because the new input differs from
the first change even though it's equal to the (true) initial value.

I also use a custom widget that displays the initial values to the user
and I don't want the intermediate changes to be displayed as initial
values in that case.
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-developers/4VrIxHRu9TU/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-develop...@googlegroups.com
> <mailto:django-develop...@googlegroups.com>.
> To post to this group, send email to django-d...@googlegroups.com
> <mailto:django-d...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/0c63b29c-dacd-45af-8caf-79229528428b%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/0c63b29c-dacd-45af-8caf-79229528428b%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

signature.asc

Yo-Yo Ma

unread,
Sep 17, 2015, 12:24:12 AM9/17/15
to Django developers (Contributions to Django itself)
To clarify, are you referring to a state where there are validation errors and the form is redisplayed, whereupon your change the value back to the original value?

Pertaining the problem you mentioned last (displaying the intermediary result): you are probably better off using the value from the form's instance for displaying outside of an input.

Moritz Sichert

unread,
Sep 17, 2015, 5:09:30 AM9/17/15
to django-d...@googlegroups.com
Am 17.09.2015 06:24, schrieb Yo-Yo Ma:
> To clarify, are you referring to a state where there are validation errors and the form is redisplayed, whereupon your change the value back to the original value?

No, there don't have to be validation errors. I want to redisplay the
form to the user even if there were none.

> Pertaining the problem you mentioned last (displaying the intermediary result): you are probably better off using the value from the form's instance for displaying outside of an input.

What do you mean by "the value from the form's instance"?
Reply all
Reply to author
Forward
0 new messages