Brad's Blog

Published: 2019-01-03

New Year, New Site (2019 edition)

blog django meta opensource

So, it's a new year (happy 2019 everyone), and despite not publishing a single article in 2018, I've done a fair amount of work recently to update my site. Here's the quick & dirty rundown: I'm now running Django 2.1 with a nod toward the next LTS release -- 2.2 django-blargg now only supports Django 2+ officially. There's still a fair number of pages powered by django-staticflatpages which has also seen a recent update ...


Published: 2017-01-03

Why is runserver/daphne so slow?

channels daphne django node node_modules react

(tl;dr) I installed django-channels and now my runserver command is very very slow. django-debug-toolbar was sort of the culprit (not really, because it was my own fault) The problem I've just started a new project using django-channels. Websockets + channels is incredibly powerful, but I noticed something strange: While running django in debug mode, the development server was incredibly slow (on the average of 60s-90s per response). After a bit of digging, it seemed that django-debug-toolbar was to blame ...


Published: 2015-10-18

A custom __date lookup for Django

database django lookups orm postgres python

⚠ Django 1.9 now includes a built-in __date lookup. If possible, you should use that instead of the code below, which doesn't support timezones. In my post last week on date lookups, I ended with a promise to take a look at building a custom django lookup (namely, a __date lookup). Django includes a basic Lookup class, and to build your own lookup expressions, all you really need to do is: Subclass django.db.models.Lookup define a ...


Published: 2015-10-10

Date lookups in Django

database django lookups orm postgres python

A while ago I tweeted out something that I've wanted to see in Django for a very long time, yet have never really taken the time to investigate or implement it: I wish #django had this: M.objects.filter(datetimefield__date=http://t.co/MVFXsN4Ivk(2015, 6, 29)) Has that ever been attempted?— Brad Montgomery (@bkmontgomery) June 29, 2015 Django's ORM has a very rich set of field lookups, but at present, it doesn't support an exact ...


Published: 2015-09-30

Django Admin Filters from ArrayFields

admin array arrayfield django filter postgresql

I've written before about the cool ArrayField support in Django, and this is another such post. In this one, we'll take a look and see how to turn your model's ArrayField values into filters in the admin. To start out, let's assume we have a model that contains a simple title (a CharField) and some keywords (an ArrayField). It might look something like this: from django.db import models class Item(models.Model): title = models.CharField ...


Published: 2015-09-03

Disabling the Forms in Django Rest Framework's Browsable API

api django djangorestframework python restframework

If you're building a RESTful api using django, then you're probably aware of Django Rest Framework. It's a great project that will do a lot of the heavy lifting for you. It's also got this really really nice featur: the browsable api. The browsable api gives you out-of-the box access to view your api, and even to interact with it using some auto-generated forms. This is great during development, because you can quickly see exaclty how ...


Published: 2015-08-26

A django iconbool filter

django filter python

Django's template laguage includes a lot of really useful built-in tags and filters, but sometimes you just need to build your own. There are many reasons why you might want to do this, but I'm lazy, and I like to build filters and tags that let me take shortcuts in the template. Here's one example of a simple filter that let's me be lazy: an iconbool filter. Motivation I really like Font-Awesome, and any time I ...


Published: 2015-04-25

Nice ArrayField widgets with choices and chosen.js

arrayfield django postgresql python

One of the really cool new features in Django 1.8 is the support for Postgres-specific fields. I'm very excited to be able to use things like PostgreSQL arrays or hstore without 3rd-party add-ons. Unfortnately, the default form inputs for ArrayFields are less than stellar. So, in this post I want to explore a few things: a Model who's ArrayField only accepts items from a set of predefined choices a ModelForm that makes use of chosen.js (which ...


Published: 2015-03-17

Django hack: Help text on a model instance

curry django functional

If you've been been working with Django for a while, you're probably familiar with the help_text attribute for model fields. It gives us a hook for adding descriptive text that gets automatically included on forms or in the admin. But what if you want to access that same information on an instance of model object? Let's look at an example! Assume we have a simple model: class BlogPost(models.Model): title = models.CharField( max_length=50, unique=True ...


Published: 2013-11-15

problems with django_extension's graph_models?

django django_extensions python

I recently ran into an issue when trying to generate an image of my project's models using django_extension's graph_models command. Unfortunately, googling for the error didn't turn up any solutions, so I'm dumping some info here (just in case!). some background For the record, I was using django_extensions, version 1.2.5 (the latest release as of this post), and Django 1.4.2 (yeah... it's old) Running the following command:$ ./manage.py graph_models my_app ...


Published: 2013-05-19

Metrics with Django and Redis

django django-redis-metrics metrics redis

So, you've got a shiny new Django-powered site, and now you'd like to start recording metrics. Perhaps you've read The Lean Startup, and you know you've got to know how people are using your site in order to know what to improve. Perhaps you're just a data nerd and you like to count things. Either way, there are ton's of ways to measure things on your site. You could use third-party applications like Google ...


Published: 2013-04-07

Django Manager Testing Woes

django managers mock models python testing

I've recently run into some strange behavior while testing some custom Django managers. While, I can't list all of the exact code (it's not open source), I'll try to list some simple examples that illustrate the problem so (hopefully), this post will be helpful for others. To get started, assume I have the following Model and Manager: class DefaultThingManager(models.Manager): def things(): # A custom method that retrieves some set of DefaultThing # objects. This doesn't ...


Published: 2012-10-30

Customizing Django's password_change view

django programming python web

If you have a site where users have the traditional username/password combination, you've got to provide some way to let users change their password. Luckily, this is fairly easy to do with Django. The auth app comes with a password_change view that does what you'd probably expect. It's also fairly easy to set up. You add a line similar to the following to your root URLConf: url(r'^accounts/', include('django.contrib.auth.urls')), You also ...


Published: 2012-09-26

Django Models & Mixins for cleaner code

django mixins programming python

I've been using Mixins lately to DRY-ly make certain behavior available to several different Django models. If you're not familiar with mixins, there's a great discussion over on StackOverflow. Here's a simple example to illustrate what I've been doing. In building Work for Pie, we've got a UserProfile model that looks something like this: class UserProfile(models.Model): user = models.OneToOneField(User) tagline = models.CharField(max_length=140) biography = models.TextField() avatar_url = models.URLField(max_length ...


Published: 2011-12-22

PostgreSQL 9.1.2 via homebrew on OS X 10.7.2

django homebrew osx postgresql python

I just picked up a snazzy new Macbook Air, and I'm working on setting up my development environment(s). For the most part this has been fairly easy. I pull in my repos from github and bitbucket, and I use virtualenv and pip to organize all my python packages (mostly installing from requirements files). Most of the other command-line tools get intalled with homebrew, and this time around I decided to install PostgreSQL with homebrew. I didn't keep ...


Published: 2011-12-13

Chosen.js in the Django admin

chosenjs django javascript jquery python web

Update Nov 23, 2013: I've written a little app (django-chosenadmin) that'll automatically add this to every app. Quite some time ago, I ran across the chosen.js plugin for jQuery and Prototype (I'm using the jQuery flavor). My first thought upon seeing this was, "This would rock in Django's admin app." Yet for some reason, I didn't make that happen.Until recently. I maintain a project where about 10 people use the admin app extensively ...


Published: 2010-11-02

SiteSprint III - The Reclaimation

django sitesprint web

My personal website is a disgrace. As a web developer, I find it embarrassing that my own site is a cobbled-together mess of PHP and static html. Only slightly less embarrassing is my blog, which is obviously hosted on Blogger. Even though Blogger has recently added a few new themes, I'm still unhappy with the look-and-feel of my blog. So, since SiteSprint III has officially started, I'm taking this opportunity to Reclaimâ„¢ my personal website.Tech Specs - the ...


Published: 2010-07-19

A case for values_list

django python web

Here's the Scenario: I have a model (lets call it Contact) with two Foreign Keys, one of which is related to User in Django's contrib.auth app. I need to build a form that lets me select an existing object, and a new user. class ContactType(Model): name = CharField(max_length=128)class Contact(Model): user = ForeignKey(User) contact_type = ForeignKey(ContactType) # possibly more fields...I need to select from existing models, so my first thought might be to build ...


Published: 2010-06-09

Vim Syntax highlighting for Apache Config Files

apache django vim

I use Django with Apache and mod_wsgi. Each project that I work on has different apache config files, so I like to keep those in the same mercurial repo that contains my django project's code.For some time now, it's been bugging me that vim doesn't do syntax highlighting for those apache configs (nor the wsgi files). I finally decided to do something about it, and I'm glad I did, because it's a fairly simple ...


Published: 2010-04-22

Pretty options for Django's auth.User

Python django web

Several of my Django Apps have Foreign Key relationships to django.contrib.auth.model.User. In Django's admin app, these show up a select elements displaying the username attribute. For some people, that may be OK, but for most of the people with which I work, it's not. We want to see prettier options, i.e. each User's full name as the options in that select element.So, here's how it works. We override the ModelChoiceField ...


Published: 2010-03-23

On select_related()

Python awesome django web

If you use Django, and your models have relationships that span across multiple tables, you need to read this: http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4select_related() is awesome.That is all.


Published: 2009-11-24

Gahhh!! Django, virtualenv, and cx_Oracle

apache cx_oracle django virtualenv

UPDATE: Thanks in advance to the comments from Graham Dumpleton whose comments below pointed me in the right direction!This wasis a plea for help.I've got django installed and configured with apache and virtualenv. I also have one particular app (named myapp) that queries an Oracle database directly (django is configured to use MySQL). All of the apps work, except for anything thatbrequires the myapp app... which includes the admin!Requesting any view that uses cx_Oracle results in ...


Published: 2009-06-30

My PYTHONPATH bit me.

Python django web

I'd just finished the first version of a new django app (myapp), and so I pushed it out to my development server. All the new code was in place, so I ran python manage.py syncdb. The result?Traceback (most recent call last): File "manage.py", line 11, in <module> execute_manager(settings) File "/usr/local/lib/python2.6/site-packages/django/core/management/__init__.py", line 340, in execute_manager File "/usr/local/lib/python2.6/site-packages/django/core/management/__init__ ...


Published: 2009-06-26

"Adding" Q objects in Django

Python django web

I've got a Django app with the following Model:class Story(models.Model): title = models.CharField(max_length=255) content = models.TextField()The Problem: I wanted to build a simple search feature that OR'ed all the search terms. Essentially, I wanted SQL resembling the following:SELECT * from myapp_stories where title LIKE '%term1%' OR content LIKE '%term1%' OR title LIKE '%term2%' OR content LIKE '%term2%'; The Solution: You can add django's ...


Published: 2009-05-06

Data Truncated Errors

Python django mysql web

I recently ran into some of the Data truncated for column ... errors in my django apps. After a little digging, I've discovered that my particular problem lie in the structure of the underlying MySQL tables. Particularly with varchar columns. I have a model that contains a FileField:class MyModel(models.Model): file = models.FileField(upload_to="files/%Y/%m/%d")Note that the MySQL table generated by this model will look something like the following:+-------------+--------------+------+-----+---------------------+----------------+| Field | Type | Null | Key | Default ...


Published: 2009-05-01

Have Apache Force File Downloads

apache django web

I have a Django app that lets users upload files. Any kind of file. It's nice that Apache will let me force file downloads based on the files extension. <LocationMatch "\.(gz|tar|pdf|docx|doc|xls|xlsx|bz2|zip)$"> SetHandler None Header set Content-Disposition attachment</LocationMatch>So, in my HTML/templates: all I have to do is this:<a href="SomFile.docx">Some File</a>


Published: 2009-04-24

Restricting Access by Group in Django

Python django web

Django's authentication system provides built-in support for Groups. When developing an app, you may want to prevent users in a particular group from accessing part of your app. For example, if you were building a tool to be used by Faculty and Students, it's quite possible that there would be parts of the app you wouldn't want Students to access (like the part that allows a User to change grades!). Luckily, there's a decorator called user_passes_test ...


Published: 2009-04-21

Dynamically Displaying Fields in a ModelForm

Programming django web

The Problem: I want to dynamically include some fields in a ModelForm based on some external criteria. Sometimes I want the fields displayed, sometimes I don't. I'm going to try to explain this scenario through a (albeit contrived) example. I have a Model that looks like the following:class Suff(models.Model): foo = models.CharField(max_length=255) bar = models.BooleanField(default=False, blank=True) def is_foo_bar(self): ''' is this model's foo attribute set to 'bar' ''' return ...


Published: 2009-03-24

mod_python checks your blood pressure

Fun Python django

I'm deploying a django project using mod_python... now, usually I will just use my package management tools to install a binary version, but this time I need to build it from source.As I get ready to go through the whole configure/make/make install process, I peruse the output of configure just to make sure everything is ok...checking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether ...


Published: 2009-02-20

Scheduled Tasks (or cron jobs) with Django

Python cron django web

This is my take on setting up cron jobs for the apps in a Django project. It is based on my own convention, and it solves my initial problems where I want to perform some action on all of my Django apps at a periodic interval (currently this is a once-a-day task).In order for this to work, I create a cron.py module for all of my INSTALLED_APPS. This module must contain a run method. Other than that, it ...


Published: 2009-01-14

A Custom form for Django's Automatic Admin.

Python django web

A huge selling-point for Django (at least for developers) is its Automatic Admin. However, the ease at which the Admin can be set up, might make one second-guess an attempt to customize what is provided by default. Of course, the default admin site may not be without its drawbacks...Many of the django Apps that I have built, tap into Django's User Authentication System. Simply put, when I build a model, it has a Foreign Key to django's ...


Published: 2009-01-05

Add a Context Processor for your Django app using Sites

Programming Python django

I've recently refactored a significant number of my Django Apps so that they include the "sites" framework. Essentially, this allows me to use the same code (and database) for multiple sites. For Example, if I was was building a CMS (and I am!), I might have a model that defines a "page":from django.db import modelsfrom django.contrib.auth.models import Userfrom django.contrib.sites.models import Siteclass Page(models.Model): title = models.CharField('title', max_length=255) content ...


Published: 2008-11-24

A simple Django example with Ajax

Javascript ajax django

I often employ Ajax in HTML forms in order to update the list of options in select elements. For example, suppose a form consists of two select elements, and the options in the second depends on the values selected in the first. A simple example of this might be an Automobile Rental website that lets you choose the type of vehicle as well as the color. Not all vehicles come in the same color, though, so you might have a ...


Published: 2008-11-19

Extending Django's MultiWidget: SplitSelectDateTimeWidget

Python django

This entry is an update to SelectTimeWidget: A custom Django Widget. The Problem: I want to use a Single widget object for a DateTimeField, but I want it to consist of select elements with appropriate options for month, day, year, hour, minute, and second. Additionally, I want to be able to specify a 12-hour format, so I would then need options for "a.m." and "p.m."Fortunately, Django's SelectDateWidget (from django.forms.extras.widgets) takes care of the ...


Published: 2008-11-18

SelectTimeWidget: A custom Django Widget

Python django

I've been meaning to write this blog post for quite some time...Django Models provide a way to create a definitive source of data for web applications. Written as a python class, a Django Model consists of Fields that (among other things) define a type for your data. Django's Forms provide a mechanism for generating HTML form elements and validating user input. A sublcass of Django's Form class is the ModelForm which essentially creates a Form based ...


Published: 2008-11-09

BarCampMemphis, the Day After

#bcmem BarCampMemphis django

I think everyone is in agreement: BarCampMemphis was a tremendous success! I want to personally thank everyone who came to my Zero-to-Wiki in 30 minutes with Django session, and I also want to remind everyone that the Presentation and the code is freely available!There were many great sessions available throughout the day, but I want to list the ones I got to attend... all of which were just excellent:Dave Barger's LinkedIn tipsWill Murphy's insights on what ...


Published: 2008-10-22

A topic for BarCampMemphis

#bcmem BarCampMemphis Python django

I've recently watched the What is BarCamp video, and I've been thinking about a Django-related topic. I'm sure there are plenty of web developers out there looking for a better way...Now, I've only been using Django for about 5 months, but I'm pretty sure I could give any interested kindred souls a good jump-start. And so that's what I'd like to do... show by example.The Example? Why not a wiki... It ...


Published: 2008-07-15

The Structure of a Django App

Programming Python django web

Previously, I'd lamented the difficultly present in choosing an web development framework. I'd worked through several symfony tutorials, and though I could see the benefits down the road, it just didn't feel right to me (yes... "feel" is a technical drawback).So, I checked out a copy of Django, and I haven't looked back. If you're the least bit proficient with python, and you need to build a database-driven web site, USE DJANGO! They have ...