Django @property executes extra database calls? Ensure using cached result instead.

People made mistakes as below code that executes database call every time it’s invoked.

class MyModel(models.Model):
    def is_ready(self):
        status = self.check_status_from_db()
        return True if status else None

Assuming that there’re 2 or more {{ object.is_ready }} in your template especially in a loop on a list page. See what would happen via django debug toolbar.

Let’s fix the issue with the below code.

class MyModel(models.Model):
    def is_ready(self):
        if not hasattr(self, '_is_ready'):
            status = self.check_status_from_db()
            self._is_ready = True if status else None
        return self._is_ready

Now, retest the code and see what’s difference.


Django ETag doesn’t work on my local machine. Why?

Django Etag doesn’t work on my local machine via the virtual webserver.

The reason is that browser sends a request via HTTP/1.1 and django sends a response via HTTP/1.0.

How I can update the version on my machine? Here’s a monkey patch that tweak the version number.

Do not use it on your web server if you use a web webserver such as apache or nginx.


# HACK: without HTTP/1.1, Chrome ignores certain cache headers during development!
#       see for a bit more discussion.
from wsgiref import simple_server
simple_server.ServerHandler.http_version = "1.1"

I found this monkey patch from but unfortunately I lost the link. 😦

.git push behavior

Git 2.0 the default behavior will be to only push the current branch checked out. The option is currently a setting called simple for option push.default.

So you can set this new (in my opinion better) default behavior right now.

Just run 

$ git config --global push.default simple

and that’s it. Just in case you want to use that. The old default, that pushed everything at once just in case you want to go back was matching, instead of simple.

.git commit messages best practice

  1. Always have a first line that references the ticket number using # notation.
  2. Blank space for the second line
  3. Additional comments after this in whatever format is best.

For example:

Fixed feature X by reticulating splines. re #1234

  • Reticulated spline for module Y and Z.
  • Small other fixes.
  • Fixed syntax and unit test issues.

Git treats the first line in a special way, especially for its summary reporting tools. We’re writing scripts using these tools. For example, to see which ticket numbers were completed in what branch, we are grepping the output of git log –pretty=oneline and having that info there would be really great.

Some examples of reasons why this is a good idea: (quoted from this blog post)
git log –pretty=oneline shows a terse history mapping containing the commit id and the summary
git rebase –interactive provides the summary for each commit in the editor it invokes
if the config option merge.summary is set, the summaries from all merged commits will make their way into the merge commit message
git shortlog uses summary lines in the changelog-like output it produces
git format-patch, git send-email, and related tools use it as the subject for emails
reflogs, a local history accessible with git reflog intended to help you recover from stupid mistakes, get a copy of the summary
gitk has a column for the summary
GitHub uses the summary in various places in their user interface

List all the files in a git repository

If you want to list all files for a specific branch, e.g. master:

$ git ls-tree -r master --name-only

-d Show only the named tree entry itself, not its children.
-r Recurse into sub-trees.

You can specify HEAD instead of master to get the list for any other branch you might be in.

If you want to get a list of all files that ever existed:

$ git log --pretty=format: --name-only --diff-filter=A | sort -

Setting up a personal git repository with dropbox

There’s a simple reference to get started using git.

~/myproject $ git init
~/myproject $ git add .
~/myproject $ git commit -m "first commit"
~/myproject $ cd ~/Dropbox/git

~/Dropbox/git $ git init --bare myproject.git
~/Dropbox/git $ cd ~/myproject

~/myproject $ git remote add origin ~/Dropbox/git/myproject.git
~/myproject $ git push -u origin master