Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:13:05+00:00 2026-06-15T20:13:05+00:00

I am trying to modify this file https://github.com/alex/django-taggit/blob/master/taggit/models.py so that when a tag is

  • 0

I am trying to modify this file https://github.com/alex/django-taggit/blob/master/taggit/models.py so that when a tag is not associated with any other object, it gets deleted.

Here’s what I added to the end of the file:

# ---

# Delete the tags that are not used by any other object
from django.db.models.signals import post_delete

def after_deleting(sender, instance, **kwargs):
    if TaggedItem.objects.filter(tag=instance.tag_id).count() == 0:
        print "Deleting tag", instance
        t = Tag.objects.get(pk=instance.tag_id)
        t.delete()

post_delete.connect(after_deleting, sender=TaggedItem)

It’s not working as expected. When I run it give this error:

Exception Type: DoesNotExist
Exception Value:    Tag matching query does not exist.

Your help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:13:06+00:00Added an answer on June 15, 2026 at 8:13 pm

    I don’t think you should be using instance.tag_id in the filter. Try just using instance.tag. Then when finding the tag object you can replace –

    t = Tag.objects.get(pk=instance.tag_id)
    

    with –

    t = instance.tag
    

    Adding _id to a field is a shortcut for getting the primary key of an object. So instance.tag is the tag object and instance.tag_id is the primary key of the tag object.

    The whole thing would be more succinct –

    # Delete the tags that are not used by any other object
    from django.db.models.signals import post_delete
    
    def after_deleting(sender, instance, **kwargs):
        if not TaggedItem.objects.filter(tag=instance.tag):
            print "Deleting tag", instance
            instance.tag.delete()
    
    post_delete.connect(after_deleting, sender=TaggedItem)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update (add/modify files) an existing JAR file, and this code (using
I have a legacy stored procedure that I'm trying to modify. This is a
I am trying to modify the example http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files to make function handleFileSelect(evt) return reader.result;
Trying to understand this open source app on github, it has a gem file:
I have a javascript file from a wordpress plugin that I'm trying to modify.
I'm trying to modify closure compiler option based on the instructions at https://play.lighthouseapp.com/projects/82401/tickets/193-javascript-require-support-in-non-minimized-files .
I'm trying to modify this: jquery desktop by adding a input field inside one
I'm trying to modify the code from this script . Basically I'm trying to
I keep getting this error while trying to modify some tables. Here's my code:
I am trying to modify the android lvl library and as part of this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.