Django管理模型继承是否可能?
管理模型中可以继承吗?
例如,考虑以下内容,
文件: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 )
我会收到以下错误:
这是堆栈跟踪 链接
问题: 该模型甚至没有出现在管理页面
额外信息:
使用 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 :
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
也许对你来说回答有点晚了,但我认为其他人也可能有类似的问题 - 就像我一样。
这是我的解决方案 - 我不确定它是否正确,但它对我有用,并且上面的其他人不能做同样的事情(假设您想要多表继承(非抽象模型),就像我一样)
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)
是的,这是可能的。我认为您所做的错误是将:
放在您的
Abstract_Admin_Model
类中。尝试不使用Meta
类。Yes it's possible. I think the error you done is to put:
in your
Abstract_Admin_Model
class. Try without theMeta
class.要使用父级的类属性,例如
list_display
或search_fields
,您可以执行以下操作:同样,您可以对其他属性(例如
actions
)执行此操作,readonly_fields
等To use the parent's class attributes, such as
list_display
orsearch_fields
you can do the following:Similarly you can do that for other attributes like
actions
,readonly_fields
, etc.问题就在这里:
这一行控制继承,所以应该是:
另外值得注意的是:您可能希望使用
TopicAdmin
而不是Admin_Topic
来更好地匹配 Django 约定。The problem is here:
This line controls the inheritance, so it should be:
Also worth noting: you may wish to use
TopicAdmin
rather thanAdmin_Topic
to better match the Django convention.:
尝试改变
Try changing:
to
修改后的 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.)