Brad's Blog

web dev, django, running, food, or whatever

Ignoring SuspiciousOperation requests for fun and (profit?) 2015-12-16

If you run a Django site, you're probably familiar with those periodic emails due to a SuspiciousOperation exception; it happens when your site receives a request that contains a host header that's now found in the ALLOWED_HOSTS setting. Yes, they're annoying, and while it's good to know when this happens, I've found that there will typically be one or two requests that you get frequently (I'm looking at you, request for /azenv.php from check.proxyradar.com). In these cases, I think it's ...

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 lookup_name attribute write the as_sql ...

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 date lookup. At least not with the syntax I would expect. Luckily, that ...

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(max_length=128) keywords = ArrayField( models.CharField(max_length=32, blank=True), default=list, blank=True, ...

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 your api works. And ...

Webucator: zip, map, and lambda 2015-08-30

Recently, the folks from Webucator, who have a series of python tutorials got in touch with me about one of my blog posts: Python's zip, map, and lambda. They wanted to turn that article into a tutorial video. I thought that sounded like a pretty cool idea, so if you're into video tutorials, give this a view. And check out some of their other tutorials as well.

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 need to represent a ...

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 I still really like!) ...

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, help_text="A unique title for this thing" ) ...

Unsubscribe 2014-12-01

This is a bit of rant. I'm currently checking (triaging, really) my email, and I've decided to click all the unsubscribe links in the marketing emails. The thing is, I've already done this. Many times. For many of these, I've just started reporting them as spam. I'm sorry, but if you ignore the fact that I've unsubscribed from your email, I'm calling you a spammer. Of course, about half the time I'm invariably met with a Server Error. JRun Connector ...