On 03/07/2008, at 11:23 AM, David Melton wrote: > > I would like to be able to change the status of an item (Yes/No Field) > in a list of items from the change-list view without having to open > the item and change it within the change form. Is there an easy way > (built in) to the Django admin framework to allow this?
Hi David, You can do this quite easily by adding a custom column to the admin list. You don't need to change any admin templates for this. It looks something like this: In models.py: def boolean_switch(field): def _f(self): v = getattr(self, field.name) url = '%d/%s/switch/' % (self._get_pk_val(), field.name) return '<a href ="%s"><img src="/media/img/admin/icon- %s.gif" alt="%d" /></a>' % (url, ('no','yes')[v], v) _f.short_description = field.verbose_name _f.allow_tags = True return _f class MyModel(models.Model): name = models.CharField() featured = models.BooleanField() featured_switch = boolean_switch(featured) featured_switch.admin_order_field = 'featured' class Admin: list_display = ['name', 'featured_switch'] In views.py: from django.db.models import get_model from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404 from django.core.exceptions import PermissionDenied def switch(request, url): """ Set/clear boolean field value for model object """ app_label, model_name, object_id, field = url.split('/') model = get_model(app_label, model_name) if not request.user.has_perm('%s.%s' % (app_label, model._meta.get_change_permission())): raise PermissionDenied object = get_object_or_404(model, pk=object_id) setattr(object, field, getattr(object, field) == 0) object.save() msg = '"%s" flag changed for %s' % (field, object) request.user.message_set.create(message=msg) return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) In urls.py, add this line before '^admin/': url('^admin/(?P<url>.*)/switch/$', 'switch', name='switch'), That should do it... I actually keep all this code in a separate app so I can apply it to any boolean field by just importing boolean_switch and including a urls module. Itai --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---