[Fixed]-Django: Using a deleted/discarded field en lieu of deleting a database row – What to do with related models?

1👍

Write a custom manager for Thing and override its get_queryset method to exclude all discarded items:

class ThingManager(models.Manager):
    def get_queryset(self):
        return super(ThingManager, self).get_queryset().exclude(discarded=True)

And update your Thing model:

class Thing( models.Model ):
    name = models.CharField(max_length=140)
    discarded = models.BooleanField( default=False )
    belongs_to = models.ForeignKey( Person, blank=True, null=True, related_name="things" )

    objects = ThingManager()

Your queries on model Thing will henceforth exclude discarded items

Leave a comment