gpt4 book ai didi

python - 如何根据 Django 中更改字段的用户自动更新字段?

转载 作者:行者123 更新时间:2023-12-01 00:07:42 27 4
gpt4 key购买 nike

上下文:

在页面的博客部分中,任何注册用户都可以发帖,但来自非 super 用户帐户的所有帖子都必须得到我在其中注册模型的管理页面上的管理员的批准。这按预期运行。

我的问题:

我怎样才能做到这一点,以便 super 用户更新博客文章(无论什么样的更改都无关紧要,例如:当管理员打开一篇文章并在文章正文中添加更多文本时,或删除文本的某些部分,因为它看起来是恶意的或虚假的),它会自动批准(批准的字段然后设置为 TRUE)?我已经考虑在模型中添加一个名为changed_by 的进一步字段,并将其初始为空。

代码:

博客/models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse


class BlogPost(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
approved = models.BooleanField(default=False)
changed_by = models.ForeignKey(User, default=None) # I am planning to use this but how?

def __str__(self):
return self.title

def get_absolute_url(self):
return reverse('blogPost-detail', kwargs={'pk': self.pk})

博客/admin.py

from django.contrib import admin
from .models import BlogPost


def approve_multiple_posts(modeladmin, request, queryset):
for blogPost in queryset:
if blogPost.approved:
blogPost.approved = False
else:
blogPost.approved = True
blogPost.save()


approve_multiple_posts.short_description = 'Change approval status'


class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'author', 'date_posted', 'approved']
actions = [approve_multiple_posts, ]


admin.site.register(BlogPost, PostAdmin)

博客/views.py

from django.shortcuts import render, get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.auth.models import User
from .models import BlogPost
from django.views.generic import (
ListView,
DetailView,
CreateView,
UpdateView,
DeleteView
)


def blog(request):
context = {
'blogPosts': BlogPost.objects.all()
}
return render(request, 'blog/blog.html', context)


class PostListView(ListView):
model = BlogPost
template_name = 'blog/blog.html'
context_object_name = 'blogPosts'
ordering = ['-date_posted']
paginate_by = 5

def get_queryset(self):
return BlogPost.objects.filter(approved=True).order_by('-date_posted')


class UserPostListView(ListView):
model = BlogPost
template_name = 'blog/user_blogPosts.html'
context_object_name = 'blogPosts'
paginate_by = 5

def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return BlogPost.objects.filter(author=user, approved=True).order_by('-date_posted')


class PostDetailView(DetailView):
model = BlogPost


class PostCreateView(LoginRequiredMixin, CreateView):
model = BlogPost
fields = ['title', 'content']

def form_valid(self, form):
form.instance.author = self.request.user
if self.request.user.is_superuser:
form.instance.approved = True
return super().form_valid(form)


class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = BlogPost
fields = ['title', 'content']

def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)

def test_func(self):
blog_post = self.get_object()
if self.request.user == blog_post.author:
return True
return False


class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
model = BlogPost
success_url = '/blog'

def test_func(self):
blog_post = self.get_object()
if self.request.user == blog_post.author:
return True
return False

如果您有任何帮助/建议,我将不胜感激!谢谢

最佳答案

在您的 PostAdmin 类中,您可以重写名为 save_model ( Django docs ) 的方法。尝试这样的事情:

class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'author', 'date_posted', 'approved']
actions = [approve_multiple_posts, ]

def save_model(self, request, obj, form, change):
important_fields = ['title', 'content'] # modify save if these fields changed
if any(x in important_fields for x in form.changed_data):
obj.approved = request.user.is_superuser
super().save_model(request, obj, form, change)

仅当用户是 super 用户时,这才会使 approved True,否则将设置为 False。

关于python - 如何根据 Django 中更改字段的用户自动更新字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59847005/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com