Kaizen Today I Learned by Ville Säävuori

Posts tagged enums

Converting Django Enumeration Types to TypeScript Enums

I added a UserEvent model to one of my personal projects as an excercise in learning about working with event-like data. One of the attributes is event_type which uses a Django models.IntegerChoices (could be models.TextChoices as well) class that looks like this: # models.py class UserEventType(models.IntegerChoices): ACCOUNT_CREATED = 100, "Account created" ACCOUNT_DELETED = 101, "Account deleted" PROFILE_UPDATED = 102, "Profile updated" PASSWORD_RESET_ASKED = 103, "Password reset asked" PASSWORD_RESET_DONE = 104, "Password reset done" PASSWORD_CHANGED = 105, "Password changed" LOGIN = 200, "Login" LOGOUT = 201, "Logout" MAIL_SUBSCRIBE = 401, "Mail subscribed" MAIL_UNSUBSCRIBE = 402, "Mail unsubscribed" I very quickly grew tired of trying to maintain the respective frontend TypeScript enums by hand so I wrote a quick & dirty management command that lets me copy paste full working code instead:

· Read the full article →