Django DB delete filtered Objects

I'm trying to delete a user's all related messages. All messages sent by all users are located in a Message Model. So, to delete a specific user's messages only, I have to filter this Message.Model and sort it by user.

def clear(request):
    user_msg = request.user
    relative_message = Message.objects.filter(user=user_msg)
    Message.objects.filter(id=relative_message).delete()

My code is not working and gives me a Value Error:

The QuerySet value for an exact lookup must be limited to one result using slicing.

You should change your code as below:

def clear(request):
    Message.objects.filter(user=request.user).delete()
Back to Top