Thank you for this, I had an idea that I could do this, was just
wondering if there was a "generic view" way of doing it.
Sawan
On Nov 27, 9:19 pm, "Jonathan Buchanan" <[EMAIL PROTECTED]>
wrote:
> On Nov 27, 2007 3:07 AM, Sawan V <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Hello,
>
> > Is there any way to use the
> > django.views.generic.create_update.update_object to update all the
> > rows in a table?
>
> > Example: my model contains a table with 2 columns, role and name. For
> > a given row, role is constant and does not change but name can.
>
> > I want to write one simple page where I can present all the data in
> > the table, have the user update names as desired and then have the
> > changes applied in one go.
>
> > Any other option besides the generic view?
>
> > Thanks
>
> > Sawan
>
> The "prefix" argument for newforms makes this easy enough:
>
> Example view, which might have taken "have the changes applied in one
> go" a bit too literally:
>
> from django import newforms as forms
> from django.db import connection, transaction
> from django.shortcuts import render_to_response
> from django.template import RequestContext
>
> from yourapp.models import RoleName
>
> class UpdateNameForm(forms.Form):
> name = forms.CharField()
>
> def __init__(role_name, *args, **kwargs):
> super(UpdateNameForm, self).__init__(*args, **kwargs)
> self.fields['name'].initial = role_name.name
> self.role_name = role_name
>
> def update_names(request):
> role_names = RoleName.objects.all()
> if request.method == 'POST':
> forms = [UpdateNameForm(rn, data=request.POST, prefix='rn%s' % rn.pk)
> \
> for rn in role_names]
> all_valid = True
> for form in forms:
> if not form.is_valid() and all_valid:
> all_valid = False
> if all_valid:
> opts = RoleName._meta
> cursor = connection.cursor()
> cursor.executemany('UPDATE %s SET %s=%%s WHERE %s=%%s' % (
> connection.ops.quote_name(opts.db_table),
> connection.ops.quote_name(opts.get_field('name').column),
> connection.ops.quote_name(opts.pk.column)),
> [(form.cleaned_data['name'], form.role_name.pk) \
> for form in forms])
> transaction.commit_unless_managed()
> else:
> forms = [UpdateNameForm(rn, prefix='rn%s' % rn.pk) for rn in
> role_names]
>
> return render_to_response('yourapp/update_names.html', {
> 'forms': forms,
> }, context_instance=RequestContext(request))
>
> Example template showing the data you'll be interested in when
> building your template:
>
> {% for form in forms %}
> {{ form.role_name.role|escape }}
> {% if form.name.errors %}{{ form.name.errors.as_ul }}{% endif %}
> {{ form.name.label_tag }} {{ form.name }}
> {% endfor %}
>
> Jonathan.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---