Given the following model:
from django.db import models
YES = "Y", "Yes"
NO = "N", "No"
the_choice = models.CharField(
refresh_from_db() alters the type of the field
>>> from refresh.models import DemoClass
>>> d = DemoClass.objects.create()
Is this behavior intentional?
I have duplicated your results. I believe the reason for the shown behavior is that the field is a CharField, not a my_choice field. In other words, the value of the field is a string, not the enum. The database has no concept of your enum class, so when it returns the field value, it is a simple string, and Django does not re-cast the value into an enum because the field is a CharField. The “choices” parameter in the field declaration only provide which values can be put into the string field, not including any identity they may have had as part of their enum. I hope this helps?
Thank you for your comments. They are in line with my thoughts. My question, however, is whether this behavior is intentional? I find it surprising that by
refresh_from_db() a change of the data type takes place. Is this behavior documented anywhere?