在 Django/Python 中将附加项目插入继承列表中
我在 Django 应用程序中使用了一些子类,并且我将继续该逻辑到我的管理实现。
目前,我有这样的管理定义:
class StellarObjectAdmin(admin.ModelAdmin):
list_display = ('title','created_at','created_by','updated_at','updated_by)
现在,我有一个 Planet 类,它是 StellarObject 的子类,带有一个附加字段。我想将此字段添加到 list_display 中(而不是完全替换 StellarObject 的显示)。
如果我尝试这样的事情:
class PlanetAdmin(StellarObjectAdmin):
list_display.insert(1,'size')
我收到以下错误:
name 'list_display' is not defined
我承认,我对 python 和继承都很陌生,所以我确信我缺少一些简单的东西。
谢谢
I'm using some subclasses in my Django app, and I'm continuing that logic through to my admin implementation.
Currently, I have this admin defintion:
class StellarObjectAdmin(admin.ModelAdmin):
list_display = ('title','created_at','created_by','updated_at','updated_by)
Now, I have a Planet class, that is a subclass of StellarObject, with an additional field. I want to add this field to the list_display (not replace StellarObject's display entirely).
If I try something like this:
class PlanetAdmin(StellarObjectAdmin):
list_display.insert(1,'size')
I get the following error:
name 'list_display' is not defined
I will admit, I'm very new to python, and inheritance in general, so I'm sure that there is something simple I am missing.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用:
此外,您还需要从
tuple
更改list_display
(即 不可变)到列表
。例如:list_display = [ ... ]
。最后,您可能会对发生的情况感到惊讶:通过插入项目,您将更改
StellarObjectAdmin
上的列表。您可能想要做的是:这将为您的
PlanetAdmin
类创建列表的新副本。发生这种情况是因为 Python 的继承方式。基本上,Python 永远不会将名称注入到命名空间中(例如,某些语言将神奇的
this
“变量”注入到方法中,而 Python 则强制您显式定义等效项 —self
—作为方法的第一个参数),并且由于class
只是另一个命名空间,因此没有任何内容(例如,其超类中的值)被注入到其中。当您有一个类
B
,它继承自另一个类A
,并且您尝试在B
上查找属性时 —B.foo
— 它首先检查foo
是否在B
的命名空间中,如果不在,则继续检查A
的命名空间,等等。我希望这是清楚的……如果没有,我可以澄清(或尝试查找相关文档)。
You'll need to use:
Also, you'll need to change
list_display
from atuple
(which is immutable) to alist
. Eg:list_display = [ ... ]
.Finally, you'll probably be surprised by what happens: by inserting the item, you're going to be changing the list on
StellarObjectAdmin
. What you probably want to do is:Which will create a new copy of the list for your
PlanetAdmin
class.This happens because of the way Python does inheritance. Basically, Python never injects names into a namespace (eg, some languages inject a magic
this
"variable" into methods, while Python forces you to explicitly define the equivalent —self
— as the first argument of methods), and since aclass
is just another namespace, nothing (like, eg, values in its super classes) gets injected into it.When you've got a class,
B
, which inherits from another class,A
, and you try to look up a property onB
—B.foo
— it first checks to see iffoo
is inB
's namespace, and if it isn't, it goes on to checkA
's namespace, and so on.I hope that's clear… If not, I can clarify (or try to find relevant documentation).
大卫是当场。另外,请注意,如果您想引用类中的变量,则需要使用“self”。
例子:
David is spot on. Also, as a note, if you want to reference the variable within the class, you'll need to use 'self.'
Example: