Django管理模型继承是否可能?

发布于 2024-12-03 17:20:11 字数 2510 浏览 1 评论 0原文

管理模型中可以继承吗?

例如,考虑以下内容,

文件: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()

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, 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 )

编辑

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

如果admin.py是像这样,我没有收到任何错误,并且模型出现在管理员上。

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


class Admin_Topic( AbstractAdminModel ):
    pass

admin.site.register( Topic , Admin_Topic )

但是如果我像这样修改它,

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


class Admin_Topic( AbstractAdminModel ):
    pass

admin.site.register( Topic , Admin_Topic )

我会收到以下错误:

the Error

这是堆栈跟踪 链接

问题: 该模型甚至没有出现在管理页面

额外信息:

使用 django 1.2.5 和 pinax 0.7.2、Ubuntu 11.04、python 2.7.1+

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 :

the 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+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

熊抱啵儿 2024-12-10 17:20:11

也许对你来说回答有点晚了,但我认为其他人也可能有类似的问题 - 就像我一样。

这是我的解决方案 - 我不确定它是否正确,但它对我有用,并且上面的其他人不能做同样的事情(假设您想要多表继承(非抽象模型),就像我一样)

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


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

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])
子栖 2024-12-10 17:20:11

是的,这是可能的。我认为您所做的错误是将:

class Meta:
    abstract = True

放在您的 Abstract_Admin_Model 类中。尝试不使用 Meta 类。

Yes it's possible. I think the error you done is to put:

class Meta:
    abstract = True

in your Abstract_Admin_Model class. Try without the Meta class.

昔梦 2024-12-10 17:20:11

要使用父级的类属性,例如 list_displaysearch_fields,您可以执行以下操作:

@admin.register(BaseClass)
class BaseClassAdmin(admin.ModelAdmin):
    list_display = ('field_a', 'field_b')
    search_fields = ('field_a', 'field_b')

@admin.register(ChildClass)
class ChildClassAdmin(BaseClassAdmin):
    def get_list_display(self, request):
        return self.list_display + ('field_c', 'field_d')

    def get_search_fields(self, request):
        return self.search_fields + ('field_c', 'field_d')

同样,您可以对其他属性(例如 actions)执行此操作, readonly_fields

To use the parent's class attributes, such as list_display or search_fields you can do the following:

@admin.register(BaseClass)
class BaseClassAdmin(admin.ModelAdmin):
    list_display = ('field_a', 'field_b')
    search_fields = ('field_a', 'field_b')

@admin.register(ChildClass)
class ChildClassAdmin(BaseClassAdmin):
    def get_list_display(self, request):
        return self.list_display + ('field_c', 'field_d')

    def get_search_fields(self, request):
        return self.search_fields + ('field_c', 'field_d')

Similarly you can do that for other attributes like actions, readonly_fields, etc.

2024-12-10 17:20:11

问题就在这里:

class Admin_Topic( admin.ModelAdmin ):

这一行控制继承,所以应该是:

class Admin_Topic( Abstract_Admin_Model ):

另外值得注意的是:您可能希望使用 TopicAdmin 而不是 Admin_Topic 来更好地匹配 Django 约定。

The problem is here:

class Admin_Topic( admin.ModelAdmin ):

This line controls the inheritance, so it should be:

class Admin_Topic( Abstract_Admin_Model ):

Also worth noting: you may wish to use TopicAdmin rather than Admin_Topic to better match the Django convention.

讽刺将军 2024-12-10 17:20:11

    class Meta:
        abstract = True

尝试改变

    class Meta:
        model = Topic
        abstract = True

Try changing:

    class Meta:
        abstract = True

to

    class Meta:
        model = Topic
        abstract = True
最好是你 2024-12-10 17:20:11

修改后的 admin.py 中的继承有效。
问题是您正在将字段“created_at”添加到管理(Admin_RSSFeed),
但它在模型上不存在(可能名为 RSSFeed?)。
(至少这是错误屏幕截图试图告诉您的。)

The inheritance in your modified admin.py works.
The problem is that you are adding the field 'created_at' to the admin (Admin_RSSFeed),
but it does not exist on the model (probably named RSSFeed?).
(At least that is what the error screenshot tries to tell you.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文