Django admin:隐藏新记录上的只读字段?
我正在使用 Django 管理站点,其中记录上有一些只读字段:
class BookAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['title', 'library_id', 'is_missing', \
'transactions_all_time']}),
]
readonly_fields = ['transactions_all_time',]
list_display = ('library_id', 'author', 'title')
这在编辑记录时非常有用 - transactions_all_time
字段是只读的,正如我想要的那样。
然而,当添加新记录时,它的行为有点奇怪。我在页面底部看到一个只读部分,我无法编辑该部分,并且此时该部分无关紧要。
如果在添加新记录时根本不存在该字段会好得多。
是否有任何 Django 选项可以在添加新记录时不显示只读字段?我知道我可以破解 add_form.html
上的 CSS 来隐藏它,但是有更好的方法吗?
谢谢。
I'm using the Django admin site with some readonly fields on records:
class BookAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['title', 'library_id', 'is_missing', \
'transactions_all_time']}),
]
readonly_fields = ['transactions_all_time',]
list_display = ('library_id', 'author', 'title')
This works great when editing records - the transactions_all_time
field is read-only, just as I want.
However, when adding new records it behaves a bit oddly. I get a read-only section at the bottom of the page, which I can't edit and which is irrelevant at this point.
It would be much better if this field was not present at all when adding new records.
Is there any Django option for not displaying read-only fields while adding a new record? I know I can hack the CSS on add_form.html
to hide it, but is there a better way?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我有类似的问题。像这样解决了
I had similar problem. Resolved it like this
这是 Kushal 解决方案的 DRY 版本:
请注意,这仅适用于主窗体 - 在内联窗体上,您传递的是主 obj,而不是内联 obj。
Here's a DRY version of Kushal's solution:
Note that this only works on the main form -- on an inline form, you're passed the main obj, not the inline obj.
我遇到了类似的问题,但解决方案略有不同。我想从“新”表单(“添加”视图)隐藏图像预览(只读字段),但在获取新对象时显示它们:
#fields
行显示原始字段。我承认这个解决方案不是很“干”,但它很清晰且简单。I had a similar problem with a slightly different solution. I wanted to hide image previews (read only fields) from the 'new' form (the 'add' view) but display them when fetching a new object:
The
#fields
line shows the original fields. I admit this solution is not very 'DRY' but it's clear and simple.另一种选择是在原始模型中设置
editable
参数。您的 ModelAdmin 将不会在版本中显示该字段,该字段将被排除。
another alternative is that you set the
editable
arg in the original Model.your ModelAdmin will not show the field in edition, the field will get excluded.
这就是我最终从“添加”页面隐藏
readonly_fields
的方式:如果没有这个,创建新对象时会显示“foo”和“bar”,并且 - 尽管是只读的 - 页面也会显示由各自的字段
默认
函数生成的值但不是保存的实际值,这令人困惑。例如,
foo = models.DateTimeField(default=django.utils.timezone.now)
显示了加载“Add”页面的时间,但生成的创建对象具有表单的时间已提交。或者,带有default=uuid.uuid4
的字段显示“添加”页面上显示的完全随机值,然后在保存时将其丢弃并替换为另一个值。readonly_fields 说:
,但情况似乎并非如此,尽管 Django 代码 似乎表明它们是 - 所以如果有人可以解释为什么我需要添加上面的
get_exclude
覆盖以获得此行为我有兴趣听到!也许“从模型表单中排除”并不一定意味着它们不在页面上?This is how I ended up hiding the
readonly_fields
from the 'Add' page:Without this 'foo' and 'bar' were shown when creating a new object and - despite being read-only - the page showed values generated by their respective field
default
functions but not the actual values that get saved which is confusing.For example
foo = models.DateTimeField(default=django.utils.timezone.now)
was showing up with the time the 'Add' page was loaded, but the resultant created object had the time the form was submitted. Or a field withdefault=uuid.uuid4
shows a completely random value shown on the 'Add' page, which is then thrown away and replaced with another on save.The documentation for readonly_fields says:
yet this does not appear to be the case, despite the Django code appearing to suggest that they are - so if anyone can explain why I need to add the above
get_exclude
override to get this behaviour I would be interested to hear! Maybe 'excluded from the ModelForm' doesn't necessarily mean they aren't on the page?