Reputation: 876
I have a large number of items and would like to be able to edit a selection of those simultaneously, while in the admin list view.
Here is screen shot of how the action checkbox could be used for this purpose. So, if I edit the importance of one selected item, the importance of all selected items should change simultaneously.
By also applying list_filter
and search_field
, this should speed up edits when dealing with a large number of items.
Upvotes: 2
Views: 2087
Reputation: 744
Found a possible solution for you:
in admin.py
,
class importanceAssignerForm(ActionForm):
importance = forms.IntegerField()
class YourAdminClass(admin.ModelAdmin):
action_form = importanceAssignerForm
def assign_importance (self, request, queryset):
for s in queryset:
s.importance = request.POST.get('importance')
s.save()
this will show up an importance field next to your actions.
dont forget to add the assign_importance function to your actions.
Upvotes: 0
Reputation: 876
Here is an example of how this can be done in javascript:
model.py
class PlaceName(models.Model):
name = models.CharField(max_length=500, db_index=True)
short_name = models.CharField(max_length=100, null=True, blank=True)
type = models.ForeignKey(PlaceType, to_field='code', null=False, blank=False, on_delete=models.PROTECT)
area = models.CharField(max_length=100, db_index=True)
lat = models.DecimalField(max_digits=9, decimal_places=6)
lon = models.DecimalField(max_digits=9, decimal_places=6)
hasl = models.IntegerField(help_text='Height above sea level')
importance = models.IntegerField(db_index=True)
place_id = models.CharField(max_length=100, unique=True)
admin.py
class PlaceNameAdmin(admin.ModelAdmin):
list_display = ('name', 'importance', 'type', 'area', 'hasl', )
list_filter = ('area', 'type', )
readonly_fields = ('name', 'short_name', 'type', 'area', 'lat', 'lon', 'hasl', 'place_id')
search_fields = ('name', 'importance', 'hasl')
list_editable = ('importance',)
class Media:
js = ('multi_line_list_edit.js',)
static/multi_line_list_edit.js
(function($) {
var value_changed = function(jQel) {
if (jQel.parent().siblings('.action-checkbox').find(':checkbox:checked').length) {
var value = jQel.val();
$('#result_list tr').each(function () {
if ($(this).find(':checkbox:checked').length)
$(this).find('td.field-importance input').val(value);
});
}
};
$(document).ready(function () {
$('#result_list td.field-importance input').change(function () {
value_changed($(this));
});
$('#result_list td.field-importance input').keyup(function () {
value_changed($(this));
});
});
})(django.jQuery);
You will still have to click the Save button, but by adding some more javascript it would be possible to save the changes just by pressing the Enter key.
Upvotes: 3