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()
creatorcontains the creator then why are you comparing the user toid?