Django管理模式继承是可能吗? [英] Django admin model Inheritance is it possible?

查看:166
本文介绍了Django管理模式继承是可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如下例所示,



文件: models.py

  class AbstractModel(models .Model):
#Meta所有类通用的信息
author = models.ForeignKey(auth.models.User,null = False,related_name =%(class)s_related_author)#创建的用户
editor = models.ForeignKey(auth.models.User,null = True,related_name =%(class)s_related_editor)#上次编辑
的用户created_at = models.DateTimeField(auto_now_add = True) #创建时间
edited_at = models.DateTimeField(auto_now = True)#修改时间

class Meta:
abstract = True


class Topic(AbstractModel):
name = models.CharField(max_length = NameMaxLength,unique = True)
version_number = models.IntegerField(default = 0)
update_frequenc y = models.IntegerField()

类似的继承在使用时似乎不会产生正确的结果 ModelAdmin



文件: admin.py

  class Abstract_Admin_Model(admin.ModelAdmin):
fields =('author','editor' ,'created_at','edited_at')
readonly_fields =('author','editor','created_at','edited_at')

def save_model(self,request,obj,form ,更改):
如果不更改:
obj.author = request.user
else:
obj.editor = request.user
obj.save()

class Admin_Topic(Abstract_Admin_Model):
fields + =('name','version_number','update_frequency')


admin.site.register (主题,Admin_Topic)



根据建议修改了上述模型,



如果 admin.py 是这样的,我没有收到任何错误,模型显示在管理员上。

  class AbstractAdminModel(admin.ModelAdmin):
pass#fields = ['author','editor','created_at','edited_at']


class Admin_Topic(AbstractAdminModel):
pass

admin.site.register(主题,Admin_Topic)

但是如果我像这样修改它

  class AbstractAdminModel(admin。 ModelAdmin):
fields = ['author','editor','created_at','edited_at']


class Admin_Topic(AbstractAdminModel):
pass

admin.site.register(主题,Admin_Topic)

我得到以下错误:





这是一个堆栈跟踪链接



问题:
该模型甚至不显示在管理页面上



Extra Info:



使用django 1.2.5与pinax 0.7.2,Ubuntu 11.04,python 2.7.1 +

解决方案

也许这是一个迟到的答案,但我认为其他人可能会有类似的问题 - 像我一样。



这是我的解决方案 - 我不确定它是否正确,但它适用于我和非上述其他可以做同样的(假设你想要一个多重继承(非抽象模型) ,就像我一样)

  class SiteEntityAdmin(admin.ModelAdmin):
fieldsets = [
,{'fields':['name']}),
]


class PhotoAdmin(SiteEntityAdmin):
fieldsets = [
照片详情',{'fields' :['photo_url','description']}),
]
fieldsets.insert(0,SiteEntityAdmin.fieldsets [0])


Is inheritance possible in admin Models ?

Like For Example consider the following ,

File : models.py

class AbstractModel ( models.Model ):
    # Meta Information common to all classes
    author = models.ForeignKey(auth.models.User , null = False ,related_name="%(class)s_related_author" ) # The user who created 
    editor = models.ForeignKey(auth.models.User , null = True,related_name="%(class)s_related_editor" ) # The user who last edited                   
    created_at = models.DateTimeField(auto_now_add  = True) # Create Time
    edited_at = models.DateTimeField(auto_now = True) # Modify Time

    class Meta:
                abstract = True


class Topic( AbstractModel ):
    name = models.CharField(max_length = NameMaxLength , unique = True)
    version_number = models.IntegerField(default = 0)
    update_frequency = models.IntegerField()

A similar inheritance does not seem to produce the correct result when used in ModelAdmin

File : admin.py

class Abstract_Admin_Model( admin.ModelAdmin ):
        fields =  ('author' , 'editor' , 'created_at' , 'edited_at')
        readonly_fields = ('author' , 'editor' , 'created_at' , 'edited_at')

        def save_model(self, request, obj, form, change):
                if not change :
                        obj.author = request.user
                else : 
                        obj.editor = request.user
                obj.save()

class Admin_Topic( Abstract_Admin_Model ):
     fields += ('name' , 'version_number' , 'update_frequency')


admin.site.register( Topic , Admin_Topic )

EDIT:

I've modified the above model based on suggestions ,

If the admin.py is like so , I don't get any error , and the model appears on the admin.

class AbstractAdminModel(  admin.ModelAdmin  ):
        pass#fields = ['author' , 'editor' , 'created_at' , 'edited_at']


class Admin_Topic( AbstractAdminModel ):
    pass

admin.site.register( Topic , Admin_Topic )

But If i modify it like so

class AbstractAdminModel(  admin.ModelAdmin  ):
    fields = ['author' , 'editor' , 'created_at' , 'edited_at']


class Admin_Topic( AbstractAdminModel ):
    pass

admin.site.register( Topic , Admin_Topic )

I get the following error :

Here is a stack trace Link

Problem : The model does not even appear on the Admin Page

Extra Info:

using django 1.2.5 with pinax 0.7.2 , Ubuntu 11.04 , python 2.7.1+

解决方案

Maybe it is bit to late for you for the answer, but I think others can have similar problem - as I did.

Here is my solution - I am not sure if it is proper, but it works for me and non other from above can do the same (assuming that you want a multitable inheritance (non abstract model), as I do)

class SiteEntityAdmin(admin.ModelAdmin):
    fieldsets = [
            (None, {'fields': ['name']}),
    ]


class PhotoAdmin(SiteEntityAdmin):
    fieldsets = [
             ('Photo details', {'fields': ['photo_url', 'description']}),
    ]
    fieldsets.insert(0, SiteEntityAdmin.fieldsets[0])

这篇关于Django管理模式继承是可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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