有没有办法在模型中表达 Django 管理设置,而不是 admin.py?
谈论 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
他们拿走了这种特殊的魔力,但是没有什么可以阻止您将 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.
也许无论如何,在模型中包含这些东西都不是一个好主意,因为当以非管理员身份使用该网站时,这会是多余的信息? (出于性能原因)
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)
在最新版本中,没有任何方法可以将模型定义中的管理选项表示为内部类。但是没有理由不能将 ModelAdmin 类放在 models.py 文件中的 Model 类之后。然后,您可以在定义之后立即调用 admin.site.register() 。
您可能会遇到模型多次调用 register() 的问题,这会生成错误。 models.py 应该只加载一次,所以这应该可以工作。如果没有,您绝对可以在 models.py 中声明 ModelAdmin 类,然后将所有的 register() 调用放在 admin.py 中。
我能想到将它们放在 admin.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:
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.
在模型中使用 Admin 类,就像使用 Meta 类一样
然后定义你想要什么
use class Admin inside your models as u use class Meta
and then define what u want