Moved to andreipetre.com

This blog is closed.

All future posts will be here http://andreipetre.com.

Wednesday, August 8, 2012   ()

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

data = self.data
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.

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

Saturday, June 30, 2012 — 1 note   ()

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):

  1. Make it easy for people using your app to find the configuration settings without too much documentation.
  2. 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

Tuesday, June 26, 2012   ()

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)

Tuesday, June 26, 2012 — 3 notes   ()

The Pokayoke Guide to Developing Software

()

Get system independent line separator in Java

System.getProperty("line.separator");

(Source: stackoverflow.com)

Friday, May 11, 2012   ()

Setting up Django Development in Eclipse with Code Complete and Graphical Debugging

(Source: vimeo.com)

()

A great guide to the impact of seafood by @NatGeo http://on.natgeo.com/dK939Y. You might want to check it before buying fish.

()

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.

Thursday, May 3, 2012   ()
Social Media Cheat Sheet

Social Media Cheat Sheet

()

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+):

  1. vim ~/.bash_profile
  2. 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)

Tuesday, April 3, 2012   ()

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

Thursday, March 22, 2012   ()

How to uninstall Xcode

sudo /Developer/Library/uninstall-devtools —mode=all

(Source: macdevelopertips.com)

Friday, March 9, 2012   ()
#America’s billion dollar #baby #scam

#America’s billion dollar #baby #scam

()

Maker's schedule vs. Manager's schedule. Which one are you on?

()