Как я могу запретить всем видеть сообщения пользователя-администратора в django?
Я пытаюсь установить систему комментирования. Проблема в том, что все пользователи могут видеть сообщения администратора. Я думал i
просмотров
from .models import Support, Comment
from .forms import SupportForm, CommentForm
@login_required
def support_details(request, id):
support = Support.objects.get(id=id)
recent_support = Support.objects.all().order_by('-created_at')
comments = Comment.objects.all()
if request.method == 'POST':
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comment = comment_form.save(commit=False)
comment.user = request.user
comment.save()
comment_form = CommentForm()
messages.success(request, 'comment posted successfully')
else:
comment_form = CommentForm()
context = {
'support': support,
'recent_support': recent_support,
'comments': comments,
'comment_form': comment_form
}
return render(request, 'support-detail.html', context)
модели
from users.models import User
from django.db import models
class Support(models.Model):
SERVICE_CHOICES = (
("Crypto", "Crypto"),
("Transaction", "Transaction"),
("Others", "Others"),
)
support_id = models.CharField(max_length=10, null=True, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
subject = models.CharField(max_length=30, null=True, blank=True)
email = models.EmailField(null=True)
related_service = models.CharField(
max_length=100, choices=SERVICE_CHOICES, null=True)
message = models.TextField(max_length=1000, null=True)
is_close = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now=True, null=True)
def __str__(self):
return self.subject
class Comment(models.Model):
comment = models.TextField(max_length=1000, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user', null=True)
support = models.ForeignKey(User, on_delete=models.CASCADE, related_name = 'comment', null=True, blank=True)
created_at = models.DateTimeField(auto_now=True, null=True)
def __str__(self):
return self.comment
шаблон
forms.py
from django import forms
from .models import Support, Comment
class SupportForm(forms.ModelForm):
class Meta:
model = Support
fields = [
'subject',
'email',
'related_service',
'message',
'is_close'
]
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = [
'comment'
]