简体   繁体   English

如何在 django 管理员中创建“仅创建”不可编辑字段

[英]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.

相关问题 创建一个仅显示(不可编辑)Django 管理字段 - Creating a display-only (non-editable) Django admin field 如何在 Django 中向自定义管理表单添加不可编辑的字段 - How do you add a non-editable field to a custom admin form in Django 如何在模型类的Flask Admin视图中使字段不可编辑 - How to make a field non-editable in Flask Admin view of a model class (Django REST)覆盖(视图)`create` 方法中的不可编辑字段 - (Django REST) Override Non-Editable Field in the (Views) `create` Method Django rest 框架仅创建序列化程序字段 - Django rest framework create-only serializer field Django管理员,使用DateTime小部件在创建时设置了不可编辑的日期字段 - Django admin, setting a non-editable date field on creation with the DateTime widget Django:如何在内联模型表单集中默认情况下使字段不可编辑? - Django: How do I make fields non-editable by default in an inline model formset? 如何在Django通用FormView中使表单字段为只读或不可编辑? - How do I make a form field read only or otherwise non editable in a django generic FormView? Django:无法为订单模型表单指定“已创建”,因为它是不可编辑的字段 - Django: 'created' cannot be specified for Order model form as it is a non-editable field django.core.exceptions.FieldError:不能为论坛 model 表单指定“日期”,因为它是不可编辑的字段 - django.core.exceptions.FieldError: 'date' cannot be specified for Forum model form as it is a non-editable field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM