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?
Advertisement
Answer
Override get_readonly_fields
in your Admin class like that:
def get_readonly_fields(self, request, obj=None): if obj: return ['source_slug'] return []