Version 2 (modified by Adam Endicott <leftwing17@…>, 10 years ago) (diff)

Updated for new model syntax (note that _id has been removed from many places in the view and template)

CookBook - Manipulator With Hidden Fields


I needed to present a form to allow a user to create a new object. However, some fields in the object were already known by the system (such as the user who was adding the object). Rather than presenting these fields to the user, I created a custom manipulator that allowed me to hide them. The example will be a user creating a new message.


Here's a simple Message model as an example:

class Message(meta.Model):
    title = meta.CharField(maxlength=255)
    body = meta.TextField()
    postDate = meta.DateTimeField(auto_now_add=True)
    user = meta.ForeignKey(auth.User)

Here's the custom manipulator that allows us to hide the user and project fields. It inherits from the model's pre-made AddManipulator, the iterates over the fields replacing some of them with a formfields.HiddenField

class MessageManipulator(messages.AddManipulator):
    def __init__(self):
        # Set everything up initially using the built in manipulator
        newFields = []
        # Iterate over all the fields looking for the ones I want to hide,
        # and replacing them with HiddenFields
        for field in self.fields:
            if field.field_name in ('user',):
                field = formfields.HiddenField(field.field_name,
        self.fields = newFields

Here's the view code:

def addMessage(request):
    manipulator = MessageManipulator()
    if request.POST:
        newData = request.POST.copy()
        errors = manipulator.get_validation_errors(newData)
        if not errors:
            newMessage =
            return HttpResponseRedirect(newMessage.get_absolute_url())
        errors = {}
        # Add the the data that you already know and don't want to burden
        # the user with here.
        newData = {'user':,}
    form = formfields.FormWrapper(manipulator, newData, errors)
    t = template_loader.get_template('addMessage')
    c = Context(request, {'form': form,})
    return HttpResponse(t.render(c))

Here's part of the template code. {{ form.user_id }} places the hidden field in the form. It will be populated with the correct data because we set that in newData above. Title and Body are other fields defined in the Message model. messages.AddManipulator took care of populating those correctly.

<form method="post" action=".">
{{ form.user }}
    <label for="id_title">Title:</label> {{ form.title }}
    {% if form.title.errors %}*** {{ form.title.errors|join:", " }}{% endif %}
    <label for="id_body">Body:</label> {{ form.body }}
    {% if form.body.errors %}*** {{ form.body.errors|join:", " }}{% endif %}

And the HTML generated from this template:

<form method="post" action=".">
<input type="hidden" id="id_user" name="user" value="2" />
    <label for="id_title">Title:</label> <input type="text" id="id_title"
class="vTextField required" name="title" size="30" value="" maxlength="255" />
    <label for="id_body">Body:</label> <textarea id="id_body"
class="vLargeTextField required" name="body" rows="10" cols="40"></textarea>


Back to Top