Django SelectDateWidget , SelectMonthYear and SelectDatetime Widgets

By default, Django comes with a SelectDateWidget which most people are familiar with. It looks like this:

from django.forms.extras.widgets import SelectDateWidget

class FooForm(forms.Form):
    date = form.(widget=SelectDateWidget())

Here are some useful snippets I’ve found if you wanted an output that is Month/Year or with time component.

  1. MonthYear
  2. DateTime

To use it, just create a new file called widgets.py (anyname will do but widgets makes most sense) in the app that would be using this widget. Import it as per normal into your forms document. If you’re going to be using both snippets in the same widgets.py document, be sure to update the __all__ field to include the classes you want to use. To use the above mentioned examples, it should look like this:

__all__ = ('MonthYearWidget', 'SplitSelectDateTimeWidget')

Things I’ve learnt in the last 2 weeks

I know django fu

Raindrops on roses and whiskers on kittens
Bright copper kettles and warm woolen mittens
Brown paper packages tied up with strings
These are a few of my favorite things

– Favourite Things, Maria

Here are some cool stuff I’ve learnt in the last couple of weeks:

  1. South: For those new to Django, one of your grievances might be the difficulty in changing the database schema. This is especially true if you’re not sure what the end product looks like. For example, if you were to change your models class definitions either by adding a column or changing the field type, syncdb will not pick this up. The workaround is to drop your table (and all the data) and run syncdb again. That sucks, enter South. What South does is that it first creates a schema of your changes and then applies it. Once you’ve set it up, it’s as simple as creating a schema like this: ./manage.py schemamigration <app_name> –auto and then applying it like this ./manage.py migrate <app_name>. Here is the tutorial link.
  2. Unit tests: Unit tests form the backbone of TDD. The idea is that you write a test to achieve a desired outcome before coding it. Django’s testing module is very similar to Python. You can read more about it here. Here are some useful links I found about Unit testing in Django.
    1. http://dougalmatthews.com/articles/2010/jan/20/testing-your-first-django-app/
    2. http://greaterdebater.com/blog/gabe/post/12
    3. http://toastdriven.com/blog/2011/apr/10/guide-to-testing-in-django/
  3. Git: I’ve been using Git for about 3 weeks now. I think I’ve got the basics nailed down and managed to merge a few conflicts as well. Here are some useful links and tools for you Git users
    1. http://kdiff3.sourceforge.net/ – to EASILY solve conflicts. sudo apt-get install kdiff3.  Launches with git mergetool
    2. http://sourceforge.net/projects/qgit/ – a visualisation of git log. It also allows you to easily retrieve old documents in the git repository. sudo apt-get install qgit. Launches with qgit
    3. http://help.github.com/git-cheat-sheets/ – a cheatsheet with all the important commands