logbook
Nested formsets with Django
I have recently needed to use nested formsets. After reading this blog post, I started coding away like the example suggests.
I however, ran into an issue that took me an hour to solve, but was quite trivial (from a solution’s point of view). I was getting an error from the management form saying that the “data is missing or has been tampered with”.
After a bit of debugging, I found the issue: passing when creating the nested formset, raises the error, even if the data is an empty dictionary. Removing it, solves the error but does not pass on data from the user to the form, for validation.
data = self.data
The solution was to only pass the data if it exists (see edit 1):
data = self.data if self.data else None
Edit 1: In order to use the empty_form feature of formsets, the above line must be changed to:
data = self.data if self.data and index is not None else None
The same applies for using uploaded files:
files = self.files if self.files and index is not None else None
Creating custom settings for your Django app
In order to configure the behaviour of various Django apps, and of Django itself, the standard approach/convention is to create setting “constants” (Python does not have constants per se, but the convention is to use UPPER CASE VARIABLES as constants).
In providing configuration settings for your app, you should (in my opinion):
- Make it easy for people using your app to find the configuration settings without too much documentation.
- Provide reasonable defaults for most/all your configuration settings.
To accomplish this, and assuming that your app is called “my_app”, I place the configuration settings in “my_app/__init__.py” as follows:
from django.conf import settings
MY_APP_SETTING_NAME = getattr(settings, 'MY_APP_SETTING_NAME', [default value])
In the rest of “my_app”, I include the configuration setting as (see edit 1):
from my_app import MY_APP_SETTING_NAME
# use MY_APP_SETTING_NAME
If MY_APP_SETTING_NAME gets re-declared in the project’s settings.py file, than that value will be used instead of my default value.
Edit 1: To eliminate dependency on app name when importing settings, use this:
from . import MY_APP_SETTING_NAME
Create a valid URL in Python
I’ve recently needed to create a valid URL by concatenating an absolute URI with a relative path. This is easily solved in Python using urlparse:
from urlparse import urljoin
absolute_uri = 'http://website.domain/'
relative_path = '/some/relative/path/'
url = urljoin(absolute_uri, relative_path)
Get system independent line separator in Java
System.getProperty("line.separator");
(Source: stackoverflow.com)
Setting up Django Development in Eclipse with Code Complete and Graphical Debugging
(Source: vimeo.com)
Idiots want to cut down forests for money in Romania
Some Romanian MPs want to change the law to allow them to cut down forests across Romania for, well it’s obvious, money.
The full story (in Romanian) is here.
So, if you wish to tell the MPs what your opinion about this is, you can find their e-mail addresses here.
Updating $PATH on Mac OS X (Lion+)
Recently I had to update my $PATH variable in order to put “/usr/local/bin” before “/usr/bin”, as indicated by Homebrew doctor.
In order to do this on Mac OS X (Lion+):
- vim ~/.bash_profile
- Add the following at the end of the file:
# Setting PATH for Homebrew
PATH="/usr/local/bin:${PATH}:/usr/local/sbin"
export PATH
(Source: jokru.org)
Quick guide to choosing a badminton racket for beginners
Here are a few points I’ve used to familiarise myself, fairly quick, with how to choose a badminton racket, for beginners:
- Weight
Do not exceed 100g. Weight is indicated by 1-4U (with 1U being the heaviest). Choose a weight of 85-90g for a control racket. - Balance point
The recommended balance point is 275-280mm with a lower balance point, i.e. the racket’s head feels lighter. - Flexibility
Choose a flexible racket (for beginners) as opposed to a stiff racket (for advanced players). - Brand
Choose one of the following brands: Head, Carlton, Ashaway, Wilson, Karakal. - Price
It’s up to you how much you want to spend.
N.B. The rackets should be no more than 680mm in length and 230mm in width.
Sources:
http://www.badminton-information.com/badminton_rackets.html
http://www.prospeed.com.my/choosing_racquets.html
http://www.pdhsports.com/buying-guides/badminton-buying-guides/bg1445.aspx
How to uninstall Xcode
sudo /Developer/Library/uninstall-devtools —mode=all
(Source: macdevelopertips.com)


