Kaizen Today I Learned by Ville Säävuori

Posts tagged django

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 →

Weeknotes 2022/17 - Software Architecture: Splitting Up Large Projects

Most non-trivial real life production apps usually require an order of magnitude more planning and configuring than the simple templates from where most projects are started from. I spent the last week testing and planning out the final configuration which would be best suited for an Django app with Vue frontend.

· Read the full article →

Encrypted Data Archives With Django And Backblaze B2

I needed to implement user data export feature to a Django project. The static and user media is handled by Backblaze B2 cloud storage (similar to Amazon S3) which also supports server side encryption that allows your data to be encrypted at rest. Usually when working with filed in Django you want to use the native storage api and storage backends.

· Read the full article →

Migrating to a Custom Django User Model Mid-Project

Using a custom user model in a Django project is almost always a good idea. The documentation for adding one when starting a project is good and clear but switching to a custom model mid-project is not that easy. This blog post explains one way, and ticket #25313 has several helpful comments.

· Read the full article →

How to migrate MySQL database to PostgreSQL

I’ve migrated a couple of old Django projects from MySQL to PostgreSQL lately and decided to document the process here to help make it go faster in the future. If your old database is not exotic in any way the migration process is pretty fast and simple. The hard part is figuring out how and how much you should tweak in the old database to get rid of warnings/errors if there are some.

· Read the full article →

Casting Types With MyPy

Typing in Python is not yet very mature. This small example displays several issues: if request.user.is_authenticated: return JsonResponse( { "user": { "uid": request.user.uid, "username": request.user.username, "email": request.user.email, } } ) The original type of request.user is Union[AbstractBaseUser, AnonymousUser] (which itself is already wrong; the user is actually a Django model that inherits from AbstractBaseUser, not AbstractBaseUser).

· Read the full article →