挑战在于:
有一个名为 GroupEditor 的组,该组的用户(GroupEditor)只能添加,更改和删除新闻 - 仅限新闻
但是要这样做,该组的用户(GroupEditor)在删除新闻时可以删除他添加的新闻
这实际上是如何实现它的问题?也许在管理面板中必须做些什么?
它是 admin.py
class PublishAdmin(admin.ModelAdmin):
fields = ['author', 'category', 'img', 'title', 'content', 'keywords', 'publish', 'active']
class CategoryAdmin(admin.ModelAdmin):
fields = ['title']
admin.site.register(Publication, PublishAdmin)
admin.site.register(Category, CategoryAdmin)
这是views.py中的方法,我用它注册了GroupEditor的用户
def register(request):
if request.user.is_authenticated(): return redirect('/')
c={}
c.update(csrf(request))
c.setdefault('form', auth.forms.UserCreationForm)
if request.POST:
newform = auth.forms.UserCreationForm(request.POST)
if newform.is_valid():
nf=newform.save(commit = False)
nf.is_staff = True
nf.save()
user=auth.authenticate(
username = newform.cleaned_data['username'],
password = newform.cleaned_data['password1']
)
group = auth.models.Group.objects.get(name = 'Redactor')
user.groups.add(group)
auth.login(request, user)
return redirect('/')
else:
c['form'] = newform
return render(request, 'register.html',c)