1. Today’s lesson: be VERY careful with programmatic use of contribute_to_class(). It doesn’t overwrite existing fields of the same name, resulting in intriguing errors when the ORM tries to do a SELECT query containing the same columns hundreds of times over…

  2. When using to send content-disposition: attachment responses, you MUST explicitly set the response encoding (charset) otherwise windows will assume the response is in whatever-weird-encoding-windows-uses, rather than UTF-8 (you are using UTF-8, aren’t you?).

    CSV example code:

    response = HttpResponse(utf_8_encoded_csv_text, status=200, mimetype="text/csv; charset=utf-8")
    response['Content-Disposition'] = 'attachment; filename=data.csv'
    

    It seems that some weird old applications like SPSS need a BOM in there as well, even though UTF-8 doesn’t have a BOM. Add that like this

    response.content = '\xef\xbb\xbf' + response.content
    

    before sending the response

    return response
    
  3. django.test.TestCase and subclasses don’t warn you if the fixtures specified in their fixtures list don’t exist — double check naming if your tests mysteriously start failing or run suspiciously quickly

  4. Loving Django’s prefetch/select_related — that, along with a few small changes, reduced a task which was taking > 500,000 queries to 4558

  5. tip: if you come across weird inconsistencies between apps when trying to serve static files in dev, run with runserver --insecure even if you have DEBUG=True