Friday, July 09, 2010

Doing ajax with django models serialization: json part

Finally a technical post after a long while, here goes

On my new job, the site requires a crazy level of interactivity, probably for the wrong reason, but lets not go there now.

Along the way, we decide to use a mix of javascript, and html, with a help of json. Thats is where, django serialization comes in. What it does is, it serialize a django queryset into xml or json. If i need to use that to generate json for django queryset, because simplejson cannot do it, not directly.

To output a queryset to json is pretty easy.


from django.core import serializers
data = serializers.serialize('json', queryset)


where queryset your djangoqueryset


in a sample database, in a called addressbook





to play around with the django shell, assume that data already there


>>> from django.core import serializers
>>> from addressbook.models import Contact
>>> serializers.serialize('json', Contact.objects.all())
[{"pk": 1, "model": "addressbook.contacts", "fields": {"phone": "012345678", "post_code": "68100", "name": "sweemeng", "address": "somewhere in selayang"}}]


To make it useful for my purpose to use in a javascript, i need to put this into a views, point your urls.py to the views, and run the server, and point your browser to it, you should able to see a json output





But here is the catch, one, this is strictly only for serializing django queryset, you can still use simplejson for other type of data. two it seems that django serializer don't serialize a single model instance.


Next post, using it with a jquery

more information on django serialization


Sunday, June 27, 2010

A day in unix-g33k @ #hackerspacekl

One of the big project of the year in hackerspacekl is unix-g33k. the goal of unix-g33k is to expose geek/geekette and non-geek alike on unix, particularly on freebsd. The workshop done in Suresh. And organize with the help of hackerspacekl particularly Sniifit and Brian Ritchie,.

The workshop covers basic OS concept for the first day, with Suresh prepared a VM running freebsd that allow us to access it via SSH for playing around. Today session itself is enlightening, while it is also serve as a reminder of the OS course i took in uni, with some extra stuff that Suresh give that involved that stuff involve the real world.

The attendee is from various background, most from IT, some runs their own business. And doing a session on a floor is a real interesting experience, kudos for the organizer for preparing more plug points for the participant, and the session is not formal, and that is always a good thing.

After the session just hang around in a coffee shop nearby. And discuss all geeky stuff.

What to look forward to for the next unix-g33k session? It involve more practical hands on freebsd. More deep stuff.

BTW the photo is here, thanks to CLchow,


**ps, no pics again, no camera sorry.

Sunday, June 06, 2010

Open Source Conference by osdc.my

MOSC 2010 is a open source conf organized by osdc.my. It will be held in  29th June to 1st June 2010) Like last year it will be held in Berjaya Time Square. Which is cool, because it is very accessible via public transport.

This year conference will have participation from a few key open source player, Red Hat and Novell is there, also a representative from the Linux Foundation will be on the conference as well. The community is represented by ubuntu.my, joomla, OWASP, wordpress, and a few others. 

This year talk have more variety than last year, it covers talk from Cloud Computing, to Security, to Uses in academics. There will be a few mini events within mosc 2010, such as Milking The Cloud competition by Microsoft, and Gecko Moving Forward by Novell. Of course there will be many community events from joomla, and wordpress among others. And not to mention show case a foss project in malaysia. 

This year conference will be a interesting conference to look forward to. And this year, I will wear a difference hat in the conference. 

Btw Check the link for more information

Malaysia Open Source Conference


Malaysia Open Source
Conference 2010 Organize By OSDC.my

Saturday, May 22, 2010

New job, now beginning(?)

So I have change my job, which is interesting in many levels.

For one it is one of the very few python development shop in malaysia, which uses a open source stack. It is the first time I involve in a enterprise development in coding side. And probably need to get used to a structured environment unlike the web startup i involved not too long ago.

First thought is, I probably need to know more of django, because they uses stuff that I didn't use before, the web startup i involve switch to a nosql store because of flexibility. The company project using django south, and uses part of django that I didn't use before. But than django can be big....

So I think that would be a interesting experience........

Tuesday, May 11, 2010

get the correct datetime value for python datetime module

UPDATE: Turn out that i did it in django shell, which, set the time to chicago, which make the time wrong in my system, the information here is right, except on the datetime.now() error. in normal python shell, or apps, datetime.now() should show the correct datetime. but on django, we need to setup the  timezone

So I have to use datetime in this project. the more I use it I realized that it can bite, unless used properly.

Let start with a simple example, start with
from datetime import datetime

from the doc, datetime.now() show today date and time.
the time i tested the line is 12:47 on 12 may 2010.
>>> datetime.now()
datetime.datetime(2010, 5, 11, 23, 46, 50, 383091)

Notice that the day and the date, is wrong. you should try it to see it yourself. Not wrong but unexpected

Run
>>> datetime.utcnow()
datetime.datetime(2010, 5, 12, 4, 56, 13, 315120)

better, but in utc time. That is in 12:56 pm on 12 may 2010

The way to solve it is to implement tzinfo class, which I didn't do, or we can use pytz
Which does it for us anyway. Run the following in the shell

import pytz
mytz = pytz.timezone('Asia/Kuala_Lumpur')
datetime.now(mytz)

after setting up the timezone with the right tzinfo. datetime.now() should show the correct datetime.

Note to self, python datetime can bite us,