在 Django/Python 中将附加项目插入继承列表中

发布于 2024-09-25 09:52:19 字数 578 浏览 3 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(2

怀中猫帐中妖 2024-10-02 09:52:19

您需要使用:

StellarObjectAdmin.list_display.insert(1, 'size')

此外,您还需要从 tuple 更改 list_display (即 不可变)到列表。例如:list_display = [ ... ]

最后,您可能会对发生的情况感到惊讶:通过插入项目,您将更改 StellarObjectAdmin 上的列表。您可能想要做的是:

list_display = list(StellarObjectAdmin.list_display) # copy the list
list_display.insert(1, 'size')

这将为您的 PlanetAdmin 类创建列表的新副本。

发生这种情况是因为 Python 的继承方式。基本上,Python 永远不会将名称注入到命名空间中(例如,某些语言将神奇的 this“变量”注入到方法中,而 Python 则强制您显式定义等效项 — self —作为方法的第一个参数),并且由于 class 只是另一个命名空间,因此没有任何内容(例如,其超类中的值)被注入到其中。

当您有一个类 B,它继承自另一个类 A,并且您尝试在 B 上查找属性时 — B.foo — 它首先检查 foo 是否在 B 的命名空间中,如果不在,则继续检查A的命名空间,等等。

我希望这是清楚的……如果没有,我可以澄清(或尝试查找相关文档)。

You'll need to use:

StellarObjectAdmin.list_display.insert(1, 'size')

Also, you'll need to change list_display from a tuple (which is immutable) to a list. 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:

list_display = list(StellarObjectAdmin.list_display) # copy the list
list_display.insert(1, 'size')

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 a class 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 on BB.foo — it first checks to see if foo is in B's namespace, and if it isn't, it goes on to check A's namespace, and so on.

I hope that's clear… If not, I can clarify (or try to find relevant documentation).

暖风昔人 2024-10-02 09:52:19

大卫是当场。另外,请注意,如果您想引用类中的变量,则需要使用“self”。

例子:

Class A:
   mylist = [1,2,3]

   def display_list(self):
      for i in self.mylist:
           print i

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:

Class A:
   mylist = [1,2,3]

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