Case-Insensitive Fields in Django Models

If you are trying to figure out how to create a case-insensitive field for your Django models, there are several ways to go about it. If you don’t care about preserving the actual mixed-case version, you can simply convert your strings to lowercase before they get saved to the model. If you are using PostgreSQL and don’t mind a database-specific solution, you can use CITextField or similar. Otherwise, read on.

Some solutions online involve adding or customizing manager or queryset functions. While this may work okay at a basic level, it isn’t a complete solution, and doesn’t actually prevent you from inserting a duplicate case-insensitive value.

I have discovered a solution that is clean, enforces case-insensitive uniqueness, and works for all Django lookups that have case-insensitive versions.

Continue reading Case-Insensitive Fields in Django Models

Combining Inherited Django Forms in the Same FormView

This week, I discovered the power of Django’s generic editing views and was eager to try them out. I had two forms; one inheriting from the other. I could have them each in their own view, but for a better user experience, I wanted it all to happen on one page by allowing the user to toggle the extra fields. Today, I’m going to show you how to combine inherited forms or models in the same view with some Django magic and a little Javascript.

Continue reading Combining Inherited Django Forms in the Same FormView

Methodic Debugging

Breathe

Shockingly, code that you or someone else wrote has been deemed imperfect and you are now responsible for fixing it. Your first instinct may be to panic. You may have a testy customer or an impatient superior who has made sure to impress upon you the importance of your swiftness. You may just not like fixing bugs. In any case, don’t be so hasty. Rushing a bug fix is a good way to create several more.

Bug fixing is a true test of your problem-solving skills. Bugs are not typically random or due to chance. They each have a logical explanation. If you approach the bug fixing process in an organized and logical manner, you will be able to find solutions in a more reasonable and predictable amount of time.

Continue reading Methodic Debugging

Simplifying Django Forms with Decorators

Overview

Django forms make getting input from your users a quick and easy process. They handle generating markup, validating input, and type-conversions; mostly leaving you to focus on your business logic. Today, I’m going to walk you through an example inspired by the Django docs, and show you how it can be standardized and simplified in order to reduce code duplication and further speed up your development process.

Continue reading Simplifying Django Forms with Decorators