#37168 closed Bug (fixed)
Readonly fields with db_default display DatabaseDefault representation
| Reported by: | Mariusz Felisiak | Owned by: | Mariusz Felisiak |
|---|---|---|---|
| Component: | contrib.admin | Version: | 6.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Readonly fields with db_default display DatabaseDefault() representation in the admin. For example:
models.pyclass MyModel(models.Model): uuid = models.UUIDField(db_default=UUID7(), editable=False, primary_key=True) created_datetime = models.DateTimeField(db_default=Now(), editable=False) modified_datetime = models.DateTimeField(db_default=Now(), editable=False) name = models.TextField()
admin.py
@admin.register(MyModel) class MyModelAdmin(admin.ModelAdmin): readonly_fields = ["uuid", "created_datetime", "modified_datetime"]
Attachments (2)
Change History (15)
by , 3 weeks ago
| Attachment: | Screenshot_20260613_125111.png added |
|---|
comment:1 by , 3 weeks ago
| Description: | modified (diff) |
|---|
by , 3 weeks ago
| Attachment: | ticket_37168.zip added |
|---|
comment:3 by , 3 weeks ago
| Triage Stage: | Unreviewed → Accepted |
|---|
Thanks for the report. The hook does sound better than a misleading __eq__()!
comment:5 by , 2 weeks ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
comment:6 by , 2 weeks ago
| Patch needs improvement: | unset |
|---|
comment:7 by , 2 weeks ago
| Needs tests: | unset |
|---|
comment:8 by , 2 weeks ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:9 by , 2 weeks ago
| Component: | Forms → contrib.admin |
|---|---|
| Patch needs improvement: | set |
| Triage Stage: | Ready for checkin → Accepted |
After review, I think adjusting the admin hooks only is safest.
comment:10 by , 2 weeks ago
| Patch needs improvement: | unset |
|---|
comment:11 by , 2 weeks ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Note:
See TracTickets
for help on using tickets.

We wanted to add
DatabaseDefault()to the list ofField.empty_valuesand modify its__hash__and__eq__. Unfortunately,expressionis required to initializeDatabaseDefaultand, TBH,DatabaseDefaultswith different expression should not be consider equal. I'm thinking about addingis_value_empty()hook to themodels.Field:and reusing it everywhere we currently check
if value in self.empty_values.