Django - 扩展另一个应用程序ModelAdmin? [英] Django - Extending another apps ModelAdmin?

查看:130
本文介绍了Django - 扩展另一个应用程序ModelAdmin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个可以使用django.contrib.comments提供的功能的项目。



CommentsAdmin ModelAdmin类有:

actions = [flag_comments,approve_comments,remove_comments] / p>

我想在我的项目中扩展CommentsAdmin ModelAdmin以包含一个动作 ban_user



我尝试在我的admin.py文件中创建自己的 NewCommentsAdmin(CommentsAdmin)对象并注册,但是我收到通知'已经注册在/ admin /''模型注释已经注册了

  class NewCommentAdmin(CommentAdmin):
actions = ['ban_user']

def ban_user(self,request,queryset):
pass

admin.site.register(Comment,NewCommentAdmin)

有没有办法做这个而不修改原来的贾ngo.contrib.comments代码?

解决方案

我想你的文件顶部有这样的东西:

  from django.contrib.comments.admin import CommentAdmin 

此导入执行模型的注册(位于此管理文件的最下方)再次



一个看起来不太好(我实际上没有尝试过)的想法可能是:

  from django.contrib .comments.models import注释
from django.contrib import admin
from django.contrib.admin.sites import NotRegistered

#尝试注销注释模型
#通过auto_discover方法注册
try:
admin.site.unregister(Comment)
除了NotRegistered:
pass

#现在我们可以加载CommentAdmin(重新注册管理模型)
from django.contrib.comments.admin import CommentAdmin

#我们必须重新注销:
try:
admin.site.unregister(评论)
除了NotRegistered:
pass

#现在你的东西...

我想这可以做得更好,但它应该工作。要使此方法正常工作,包含此文件的应用程序必须在 INSTALLED_APPS 中的注释应用程序之后。



现在到你的班我想如果你写了 actions = ['ban_user'] ,你实际上会覆盖父类中的所有操作。我认为这是覆盖 get_actions 方法的最简单的方法:

  NewCommentAdmin(CommentAdmin):

def get_actions(self,request):
actions = super(NewCommentAdmin,self).get_actions(request)

#逻辑在这里基于request.user如果你想要
#限制新的行动到某些用户
actions.append('ban_user')

返回动作

def ban_user(self,request,queryset):
pass

admin.site.register(Comment,NewCommentAdmin)

Is there a way to extend another apps ModelAdmin?

I have a project that uses functionality offered by django.contrib.comments.

The CommentsAdmin ModelAdmin class has:
actions = ["flag_comments", "approve_comments", "remove_comments"]

I would like to extend the CommentsAdmin ModelAdmin in my project to include an action ban_user.

I've tried creating my own NewCommentsAdmin(CommentsAdmin) object in my admin.py file and registering it, but I get a notice 'AlreadyRegistered at /admin/' 'The model Comment is already registered'.

class NewCommentAdmin(CommentAdmin):
    actions = ['ban_user']

    def ban_user(self, request, queryset):
        pass

admin.site.register(Comment, NewCommentAdmin)

Is there a way to do this without modifying the original django.contrib.comments code?

解决方案

I guess you have something like this at the top of your file:

from django.contrib.comments.admin import CommentAdmin

This import executes the registration of the model (at the very bottom of this admin file) again.

One idea that doesn't look very nice (I actually haven't tried it) could be:

from django.contrib.comments.models import Comment
from django.contrib import admin
from django.contrib.admin.sites import NotRegistered

# Try to unregister the Comment model 
# that was registered via the auto_discover method
try:
    admin.site.unregister(Comment)
except NotRegistered:
    pass

# Now we can load the CommentAdmin (which reregisters the admin model)
from django.contrib.comments.admin import CommentAdmin

# We have to unregister again:
try:
    admin.site.unregister(Comment)
except NotRegistered:
    pass

# Now your stuff...

I guess this could be done better but it should work. To make this approach work, the application that contains this file has to be after the comments application in INSTALLED_APPS.

Now to your class. I think if you write actions = ['ban_user'] you actually overwrite all the actions in the parent class. I think it is the easiest way to override the get_actions method:

class NewCommentAdmin(CommentAdmin):

    def get_actions(self, request):
        actions = super(NewCommentAdmin, self).get_actions(request)

        # Do some logic here based on request.user if you want 
        # to restrict the new action to certain users
        actions.append('ban_user')

        return actions

    def ban_user(self, request, queryset):
        pass

admin.site.register(Comment, NewCommentAdmin)

Hope that helps (or at least gives an idea) :)

这篇关于Django - 扩展另一个应用程序ModelAdmin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆