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 3876256
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:21:39+00:00 2026-05-19T22:21:39+00:00

I have created a model which attaches media to blog posts: class MediaAttachment(models.Model): media

  • 0

I have created a model which attaches media to blog posts:

class MediaAttachment(models.Model):
    media = models.ForeignKey(Media, related_name='attachment')
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField(db_index=True)
    object = generic.GenericForeignKey('content_type', 'object_id')

It works well, but when if I attach some media to a blog entry and then delete that blog entry, the attachment to that entry remains in the MediaAttachment table. What would be the smartest way to GC the MediaAttachment tables? Is signals the best way to do this?

  • 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-05-19T22:21:39+00:00Added an answer on May 19, 2026 at 10:21 pm

    Ok, so I managed to make this work nicely using the following method.

    I added this signal to my MediaAttachment model:

    post_delete.connect(MediaAttachment.objects.delete_attachment)
    

    Then my delete method looks like this:

    def delete_attachment(self, instance, *args, **kwargs):
        ctype = ContentType.objects.get_for_model(instance)
        return self.filter(content_type=ctype, object_id=instance.pk).delete()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java Swing I have created a JTable which uses a table model class
I'm starting out using the Zend Framework and have created a suitable model which
I'm putting together an MVC application where I have created a base Model which
HI , I have created a model , which adds some fixture and model
I have created a TableRow layout model which I'm using as a template for
I have a controller called articles , which creates the articles model which gets
I have two models: Person and Address which I'd like to create in a
if i have created a view model and have a partial form that is
I have created a Dynamic Data site against an Entity Framework Model I have
I have an Entity Data Model that I have created, and its pulling in

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.