missing default values in formfields.FormWrapper(...)

8 views
Skip to first unread message

Emanuele

unread,
Dec 4, 2005, 6:16:52 PM12/4/05
to Django users
Dear djangonauts,
I'm playing with formfields and manipulators: wonderful! But I'm
missing one thing: following docs on Forms and Manipulators I'm not
able to put model's default values to fill forms in the proper way.

Consider the last example of "Using the AddManipulator" subsection:
------------
def create_place(request):
manipulator = places.AddManipulator()
if request.POST:
# If data was POSTed, we're trying to create a new Place.
new_data = request.POST.copy()
........(cut).........
else:
# No POST, so we want a brand new form without any data or
errors.
errors = new_data = {}
# Create the FormWrapper, template, context, response.
form = formfields.FormWrapper(manipulator, new_data, errors)
return render_to_response('places/create_form', {'form': form})
---------------
from what is written, when create_place is called with no POST data,
the "form" has new_data={}, so default values are missing (assume that
Place has some explicit default values in the model).
I'd like to use model's default values in that form, basically filling
new_data={} automagically from model's definitions. How can I do it?
Right now I'm only able to do it "by hand", like writing explicitely
new_data={'city':'NoWhere', 'zip_code':'12345'} instead of new_data={},
which is not so nice.

Thanks in advance for every ideas/suggestions to solve this problem,

Emanuele

Note: my request looks like in some way to Bryan Murdock's request
(unanswered):
http://groups.google.it/group/django-users/browse_thread/thread/e97eeed0b3256564/355a55a1ff7e4a61?q=form+default+values&rnum=1#355a55a1ff7e4a61

Pedro Furtado

unread,
Dec 4, 2005, 7:16:04 PM12/4/05
to django...@googlegroups.com
    else:
        # No POST, so we want a brand new form without any data or
errors.
        errors = new_data = {}

Copy from the admin view:

    else:
        # Add default data.
        new_data = manipulator.flatten_data()

        # Override the defaults with request.GET, if it exists.
        new_data.update(request.GET)
        errors = {}

Hope it works.

Bye
--
Pedro

Emanuele

unread,
Dec 5, 2005, 4:48:52 AM12/5/05
to Django users
Thanks a lot! It works perfectly. I think this should go in 'Forms and
Manipulators' docs as soon as possible!

Emanuele

Emanuele

unread,
Dec 5, 2005, 7:08:12 PM12/5/05
to Django users
Maybe this is relevant for those interested in the thread:
manipulator.flatten_data() is available only on recent SVN version of
Django. It is unfortunately NOT available in Django_0.90.

Cheers,

Emanuele

Bryan Murdock

unread,
Dec 7, 2005, 2:51:48 AM12/7/05
to django...@googlegroups.com

I just did an svn up. Lo and behold, my related problem has
disappeared. It seems the generic views now do this for me, like they
should. Yay.

Bryan

Reply all
Reply to author
Forward
0 new messages