简体   繁体   中英

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. 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

class Fonte(Always):
    source_slug = models.CharField(max_length=255)

admin.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.

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. Is there any more elegant way?

Override get_readonly_fields in your Admin class like that:

def get_readonly_fields(self, request, obj=None):
    if obj:
        return ['source_slug'] 
    return []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM