David Hollenbeck
David Hollenbeck

Reputation: 121

Delete an object by id with django

Working on school project. I am trying to delete messages by only the people that were logged in and have created it. I want other user to see them but only the creator can delete it. I have code that will delete everything in the database. I researched and found that Mysecret.objects.filter(id=request.session['user_id']).delete() should work but when i do this the page wont delete anything just refreshes the page.

I will only post views and model as i know everything else works. I think its just the format that I cant seem to nail down.

SO what I have gotten up to now is I have my message id which is secret.id and i have the creators id which is secret.creator.id and the session id request.session['user_id']. how do i compare them to delete only tha tmessage

Views.py 

from django.shortcuts import render, redirect
from . models import Mysecret
from ..logReg.models import User


# Create your views here.

def index(request):
    context = {

    "secret": Mysecret.objects.all(),

    }

    return render(request, 'secretdojo/index.html', context)


def create(request):
    secreteid= User.objects.get(id=request.session['user_id'])

    Mysecret.objects.create( secret=request.POST['message'], creator=secreteid)

    return redirect( 'secretdojo:index')

def removesecret(request):


    Mysecret.objects.filter(id=request.session['user_id']).delete()



    return redirect( 'secretdojo:index')


def topsecret(request):
    context = {


    }

    return redirect( '/')

model.py 


from __future__ import unicode_literals
from django.db import models
from ..logReg.models import User

class Mysecret(models.Model):
    secret = models.CharField(max_length =500)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)
    loguser = models.ManyToManyField(User, related_name='loguser')
    creator = models.ForeignKey(User, related_name='creator')

The foreignKey part of the User model:

class User(models.Model):
    first_name = models.CharField(max_length = 50)
    last_name = models.CharField(max_length = 50)
    email = models.CharField(max_length = 100)
    password = models.CharField(max_length =100)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    objects = UserManager()

Upvotes: 0

Views: 2886

Answers (5)

Shubham Patil
Shubham Patil

Reputation: 9

To Delete a specific product from the session cart by id:

` def DeleteProductView(request,id):

cart = request.session.get('cart')
product = Product.objects.get(id=id)
var = str(product.id)
if request.method == 'POST':
    request.session.modified = True
    cart.pop(var)`

Upvotes: 0

David Hollenbeck
David Hollenbeck

Reputation: 121

The answer is as follows.

mainid = Mysecret.objects.get(id=id)
userid =User.objects.get(id=request.session['user_id'])

if mainid.creator != userid:
    return False
mainid.delete()

 <td>{{ dog.created_at }}</td>
                {% if dog.creator.id == request.session.user_id %}
                <td><form action="{% url 'secretdojo:remove' id=dog.id %}">{% csrf_token %}<input class="btn btn-default" type="submit" value="Remove"></form></td> 
                {% endif %}
            </tr>
            {% endfor %}
        </table>

Upvotes: 0

Gagik Sukiasyan
Gagik Sukiasyan

Reputation: 856

Suggestion: don't keep your own User model, but use the Django's Authentication system

In terms of your code I believe you "create" code also doesn't work and it is because of this line Mysecret.objects.filter(id=request.session['user_id']). As this will not return anything. You are searching in model Mysecret, but with User ID, which will not return anything. Instead you need to have:

 Mysecret.objects.filter(creator=request.session['user_id'])

If you use Danjgo's Auth, you will have request.user and can do following:

 Mysecret.objects.filter(creator=request.user)

One other note: What is the reason of having loguser field for Mysecret model? It seems redundant for your case,

Upvotes: 1

vijay
vijay

Reputation: 886

To delete all the messages created by creator

Mysecret.objects.filter(creator=request.session['user_id']).delete()

To delete message by id and only by its creator

eg: assume secret_id is the id of the message

Mysecret.objects.filter(creator=request.session['user_id'],id =secret_id ).delete()

Upvotes: 0

BugHunter
BugHunter

Reputation: 1204

You should something like this :

Mysecret.objects.filter(creator=request.user).delete()

Upvotes: 1

Related Questions