jixad
jixad

Reputation: 65

Delete object from database automatically if one field is TRUE

Automatically delete an object from the database if one attribute of the object is TRUE.

I've tried Django Signals, but it didn't help.

class Question(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField(max_length=50)
    question = models.TextField(max_length=200)
    answered = models.BooleanField(default=False)

    def __str__(self):
        return self.name

If I change the "answered" field to TRUE in Admin Panel, then this object must be automatically deleted from the database.

Upvotes: 1

Views: 347

Answers (1)

cagrias
cagrias

Reputation: 1847

You will need post_save signals by using something like:

from .models import Question

from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=Question)
def save_profile(sender, instance, **kwargs):
    if instance.answered:
        instance.delete()

Upvotes: 3

Related Questions