有没有办法在模型中表达 Django 管理设置,而不是 admin.py?

发布于 2024-08-15 13:05:41 字数 119 浏览 5 评论 0原文

谈论 Django 1.1.1。我一度认为(0.96)admin.py 文件中放入的内容是模型内部类的一部分。

将所有这些都集中在一个地方有一种美感。但我不知道这个改变是否是出于必要。有什么令人信服的理由吗?

Talking about Django 1.1.1. I thought at one time (0.96) the kinds of things put inside of the admin.py file were part of an inner class of the model.

There's a certain beauty in having all of this in one place. But I don't know if this change was out of necessity. Any compelling reasons one way or the other?

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

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

发布评论

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

评论(4

温柔少女心 2024-08-22 13:05:41

他们拿走了这种特殊的魔力,但是没有什么可以阻止您将 admin.ModelAdmin 子类放在 models.Model 子类本身之后。我更喜欢自己将它们放在一起,因为我不太可能忘记向列表中添加一个字段以在管理显示屏中显示。

They took away that particular magic, but there is nothing to keep you from putting your admin.ModelAdmin subclass right after the models.Model subclass itself. I prefer keeping them together myself because it's less likely I'll forget to add a field to the list to show in the admin display.

凡尘雨 2024-08-22 13:05:41

也许无论如何,在模型中包含这些东西都不是一个好主意,因为当以非管理员身份使用该网站时,这会是多余的信息? (出于性能原因)

Perhaps it would not be a good idea to have this stuff in the models anyway, since it would be excess information when using the site as a non-admin? (For performance reasons)

不打扰别人 2024-08-22 13:05:41

在最新版本中,没有任何方法可以将模型定义中的管理选项表示为内部类。但是没有理由不能将 ModelAdmin 类放在 models.py 文件中的 Model 类之后。然后,您可以在定义之后立即调用 admin.site.register() 。

您可能会遇到模型多次调用 register() 的问题,这会生成错误。 models.py 应该只加载一次,所以这应该可以工作。如果没有,您绝对可以在 models.py 中声明 ModelAdmin 类,然后将所有的 register() 调用放在 admin.py 中。

我能想到将它们放在 admin.py 中的几个原因是:

  • 约定 - 似乎这正在成为最佳实践。
  • 解耦——管理定义与模型没有太大关系。
  • 干净——可能不需要用你不会做太多改变的东西来填充你的 models.py 文件。

但是,如果您的 models.py 文件不会很长,我可以看到将它们彼此相邻的吸引力。

There isn't any way to express the admin options inside the model definition as an inner class in the latest version. But there is no reason why you can't put your ModelAdmin class right after your Model class in your models.py file. You can then just call your admin.site.register() right after your definition.

You may run into a problem with the register() being called more than once for a model, which will generate an error. models.py should only get loaded once though so this should work. If not, you can definitely declare your ModelAdmin class in models.py, and then put all your register() calls in admin.py.

A couple reasons that I can think of to put it them in admin.py are:

  • Convention -- seems like this is becoming a best practice.
  • Decoupling -- the admin definitions don't really have much to do with the model.
  • Cleanness -- probably no need to fill up your models.py file with stuff you aren't going to change much.

But if your models.py file isn't going to be very long I can see the attraction of having them right next to each other.

傲娇萝莉攻 2024-08-22 13:05:41

在模型中使用 Admin 类,就像使用 Meta 类一样
然后定义你想要什么

use class Admin inside your models as u use class Meta
and then define what u want

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