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
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
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
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
from urlparse import urljoin
absolute_uri = 'http://website.domain/'
relative_path = '/some/relative/path/'
url = urljoin(absolute_uri, relative_path)
Setting up Django Development in Eclipse with Code Complete and Graphical Debugging
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
Here are a few points I’ve used to familiarise myself, fairly quick, with how to choose a badminton racket, for beginners:
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.
Choose a flexible racket (for beginners) as opposed to a stiff racket (for advanced players).
Choose one of the following brands: Head, Carlton, Ashaway, Wilson, Karakal.
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.
sudo /Developer/Library/uninstall-devtools —mode=all