[英]How to make a “create-only” non-editable field in django admin
im looking for some solution about make a "create-only" field on django admin using models.我正在寻找一些关于使用模型在 django 管理员上创建“仅创建”字段的解决方案。 I saw some questions before, but no one can answer the core question: the field should appear when the user are creating on admin panel, but i dont want to be able to edit.
我之前看到过一些问题,但没有人能回答核心问题:当用户在管理面板上创建时,该字段应该出现,但我不想能够编辑。
models.py模型.py
class Fonte(Always):
source_slug = models.CharField(max_length=255)
admin.py管理员.py
@admin.register(Fonte)
class FonteAdmin(admin.ModelAdmin):
readonly_fields = ['source_slug']
the "readonly_fields" solves the problem in the matter of editing in the future, but ends up forbidding when creating. “readonly_fields”解决了以后编辑的问题,但在创建时却被禁止了。
Problem: Im using this field to make a hash and i dont wanna this change evermore.. I thought about using a second field that would generate a hash on top of the editable one field in the creation, after that the field would be "dead", but that seems to me to be contrary to the 2 way of normalization.问题:我使用这个字段来制作 hash 并且我不想再进行这种更改了.. 我考虑过使用第二个字段来生成 hash 在创建中可编辑的一个字段之上,之后该字段将是“死的” ",但在我看来,这与 2 规范化方式相反。 Is there any more elegant way?
有没有更优雅的方式?
Override get_readonly_fields
in your Admin class like that:像这样覆盖管理员
get_readonly_fields
中的 get_readonly_fields :
def get_readonly_fields(self, request, obj=None):
if obj:
return ['source_slug']
return []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.