Blog

TIL: DEBUG=True will eat all your memory

So, there I was synchronising several thousand database records with a 3rd party server. Off I go to take care of some serious business while I wait for the sync to complete. But when I return instead of seeing a nicely up-to-date database, my machine is thrashing so hard it's almost completely unresponsive. Turns out Python is eating up 80% of the memory. So I pile into optimising the code that does the syncing. No luck there. (I mean, the code's better but Python still eats all the memory.) So I figure I better take a look at exactly what's taking up all that space. While looking for some memory profiling tools, I come across this post on debugging memory leaks in Django.

Read more →

October 12, 2012

How to use sessions in Django unit tests

If you’ve tried using sessions with the Django test client as the official documentation describes, you'll have noticed that it doesn't work. There are a couple bugs in the Django ticket system around this, the main one being https://code.djangoproject.com/ticket/10899, but there's been no movement on it for 8 months. However, that ticket has the code you need to get your sessions working. So if you want to use sessions in your tests (e.g., to test whether a view is adding the right stuff to the session), add the following to the setUp() method of your test case:

Read more →

September 5, 2012

How to turn off CSRF protection for class based views

It's gotta go on the .dispatch() method, not on .post() (or .get()). class AwesomeView(DetailView): @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(AwesomeView, self).dispatch(*args, **kwargs) def post(self, request, *args, **kwargs): print request.POST return HttpResponse('Aw yeah!')

Read more →

September 3, 2012

Dynamic form generation with class based generic views

Recently I had a situation where I wanted to use a form to gather some input, and use that input to generate a second form. Neither form needed to interact with the local database, since they were just talking to a 3rd party API behind the scenes. This is one of those situations that is not a primary use-case for Django (read as: near impossible to find an example of how to do it.).

Read more →

August 19, 2012

Peace of mind

One of my favourite things about unit tests is the peace of mind they afford me. Of course, tests don't guarantee bug-free code, but they definitely contribute to peace of mind. That's especially true when you're altering existing code, whether making functional changes or refactoring. Peace of mind isn't at all superficial to technical work. It's the whole thing. That which produces it is good work and that which destroys it is bad work. The specs, the measuring instruments, the quality control, the final checkout, these are all means toward the end of satisfying the peace of mind of those responsible for the work. What really counts in the end is their peace of mind, nothing else.

Read more →

July 29, 2012

Using Django\'s == and in operators on unsaved Model instances

tldr; Don't rely on the == or in operators before hitting .save() on your model instances. This got me pretty good recently. Say we have a Django model, Animal, to which we can assign various species. Here's our models.py: from django.db import models class Animal(models.Model): species = models.CharField(max_length=100) Now, say you're writing some unit tests like a good developer, and you want to compare two instances of a Model:

Read more →

July 18, 2012

Django JSON fixture syntax for ManyToMany and ForeignKey fields

fixture = [{"pk": 1, "model": "library.Book", "fields": {"authors": [author_primay_key_1, author_primary_key_2, author_primary_key_3], "publisher": publisher_primary_key}}] Python: 2.6.5 Django: 1.2.0

Read more →

April 29, 2012

What to do if Selenium WebDriver won't load anything when you call .get(url)

Rage. Search the internet for two days trying to find other people that have the same problem. Lament the fact that right now you have to work on a crippled Windows XP machine. Stumble on an old bug report that implies the problem might be with your proxy settings. Reflect on how problematic the corporate proxy which you're stuck behind has been. Rage a little more. Open IE. Go to Tools > Internet Options > Connections (tab) > LAN settings (button) > Advanced (button under Proxy Settings). Append 127.0.0.1\* to the list of Exceptions. You’re welcome.

Read more →

April 29, 2012

Installing node.js and jasmine-node

There seem to be a plethora of ways to install node.js, and none of them worked initially for me. Even when I got node itself working, npm (node's creatively named package manager) would refuse to install. Or if I got it installed, it would then refuse to install jasmine-node. So here's what I did to finally get it all working: Install node.js At the time I wrote this post, the development version of node was failing to build. So, rather than installing the latest version from git, I had to install from the last stable version:

Read more →

December 17, 2011