Brad's Blog

Why is runserver/daphne so slow? 2017-01-03

(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 ...

A custom __date lookup for Django 2015-10-18

⚠ 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 ...

Date lookups in Django 2015-10-10

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 ...

Django Admin Filters from ArrayFields 2015-09-30

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 ...

Disabling the Forms in Django Rest Framework's Browsable API 2015-09-03

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 ...

A django iconbool filter 2015-08-26

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 ...

Nice ArrayField widgets with choices and chosen.js 2015-04-25

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 ...

Django hack: Help text on a model instance 2015-03-16

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 ...

problems with django_extension's graph_models? 2013-11-15

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 ...

Metrics with Django and Redis 2013-05-18

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 ...

Django Manager Testing Woes 2013-04-07

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 ...

Customizing Django's password_change view 2012-10-30

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 ...

Django Models & Mixins for cleaner code 2012-09-26

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 ...

PostgreSQL 9.1.2 via homebrew on OS X 10.7.2 2011-12-22

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 ...

Chosen.js in the Django admin 2011-12-13

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 ...

SiteSprint III - The Reclaimation 2010-11-02

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 ...

A case for values_list 2010-07-19

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 ...

Vim Syntax highlighting for Apache Config Files 2010-06-09

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 ...

Pretty options for Django's auth.User 2010-04-22

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 ...

On select_related() 2010-03-23

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.

Gahhh!! Django, virtualenv, and cx_Oracle 2009-11-24

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 ...

My PYTHONPATH bit me. 2009-06-30

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__ ...

"Adding" Q objects in Django 2009-06-26

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 ...

Data Truncated Errors 2009-05-06

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 ...

Have Apache Force File Downloads 2009-05-01

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>

Restricting Access by Group in Django 2009-04-24

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 ...

Dynamically Displaying Fields in a ModelForm 2009-04-21

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 ...

mod_python checks your blood pressure 2009-03-24

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 ...

Scheduled Tasks (or cron jobs) with Django 2009-02-20

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 ...

A Custom form for Django's Automatic Admin. 2009-01-14

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 ...

Add a Context Processor for your Django app using Sites 2009-01-05

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 ...

A simple Django example with Ajax 2008-11-24

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 ...

Extending Django's MultiWidget: SplitSelectDateTimeWidget 2008-11-19

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 ...

SelectTimeWidget: A custom Django Widget 2008-11-18

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 ...

BarCampMemphis, the Day After 2008-11-09

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 ...

A topic for BarCampMemphis 2008-10-22

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 ...

The Structure of a Django App 2008-07-15

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 ...