Django admin model Inheritance is it possible?(Django 管理模型继承是否可能?)
问题描述
管理模型中是否可以继承?
Is inheritance possible in admin Models ?
例如考虑以下,
文件: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 )
编辑:
我根据建议修改了上面的模型,
I've modified the above model based on suggestions ,
如果 admin.py
是这样的,我没有收到任何错误,并且模型出现在 admin 上.
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 )
但是如果我这样修改它
class AbstractAdminModel( admin.ModelAdmin ):
fields = ['author' , 'editor' , 'created_at' , 'edited_at']
class Admin_Topic( AbstractAdminModel ):
pass
admin.site.register( Topic , Admin_Topic )
我收到以下错误:
这是一个堆栈跟踪 链接
问题:该模型甚至没有出现在管理页面上
Problem : The model does not even appear on the Admin Page
额外信息:
使用 django 1.2.5 和 pinax 0.7.2、Ubuntu 11.04、python 2.7.1+
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 管理模型继承是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django 管理模型继承是否可能?
基础教程推荐
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01