Skip to content

Commit

Permalink
adding an admin action to remove orphaned tags
Browse files Browse the repository at this point in the history
  • Loading branch information
guel-codes committed Aug 15, 2024
1 parent 764664f commit 80891f9
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion taggit/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TagAdmin(admin.ModelAdmin):
ordering = ["name", "slug"]
search_fields = ["name"]
prepopulated_fields = {"slug": ["name"]}
actions = ["render_tag_form"]
actions = ["render_tag_form", "remove_orphaned_tags_action"]

def get_urls(self):
urls = super().get_urls()
Expand Down Expand Up @@ -84,3 +84,9 @@ def merge_tags_view(self, request):
"selected_tag_ids": selected_tag_ids,
}
return render(request, "admin/taggit/merge_tags_form.html", context)

@admin.action(description="Remove orphaned tags")
def remove_orphaned_tags_action(self, request, queryset):
orphaned_tags = Tag.objects.filter(taggit_taggeditem_items=None)
count, _ = orphaned_tags.delete()
self.message_user(request, f"Successfully removed {count} orphaned tags.", level="success")

0 comments on commit 80891f9

Please sign in to comment.