[Answer]-How to delete a record by using a class method?

1👍

You can add class method this way, decorate method with classmethod:

class Sends(models.Model):
    ...

    @classmethod
    def delete_entrie(cls, del_id):
        cls.objects.filter(id=del_id).delete()

Call it:

Sends.delete_entire(id_)
👤iMom0

0👍

There already is a method, delete, on models in Django.

So your view could look like::

def admin_sends(request):
    if request.method == 'POST':
        post = request.POST
        Sends.objects.get(id=post['delete']).delete()

    ...

Meaning “get the object with the matching ID and call the delete method on it.”

Leave a comment