Archive for May, 2008
Handling ManyToMany field in Django
I’m still learning Django; but as I learn more and more I admire the thoughtfulness of those who designed the framework. They’ve surely made complex things easy and simple things easier.
I continue to learn Django by creating applications that I will use – either at office or at home. First I created SOL (a microblog) to be used at office; Now I’m developing a book inventory application for my own use.
In this application, I have a M2M model – book and authors.
name = models.CharField(max_length=45)
class Book(models.Model):
...
authors = models.ManyToManyField(Author, verbose_name="Authors")
...
Django’s Many-to-Many relation documentation is pretty detailed. I couldn’t find much details on using M2M in a template. However, it was explained in Google Group.
<td class="itemcol"> {{item.title}}
</td><td class="itemcol">
{% for author in item.authors.all %} <br />
{{author.name}}
{%endfor %}</td></tr>
{% endfor %}</p>
If you are implementing AJAX, you may want to compile the rows into a list. That can be achieved by:
'cell': [book.title, ' & '.join([auth.name for auth in book.authors.all()]), book.publisher, book.isbn]} for book in books]
Don’t know if that’s all to M2M. But this all I’ve learnt.
Building a Book Inventory in Django
I’m a voracious reader. With such a hobby I’ve gotten loads of books. Many-a-times, I tried to take an inventory of the books but failed, primarily because of laziness. With the recent passion with Django, I thought why not build something that will help me learn Django as well as which will be useful to me. If it comes out well, it may be useful to others as well.
So far it has gone on well. I’ve learnt about the below aspects as I built iCat (I got to work around the name that I came up with, because there was already a project in Sourceforge with the name that I needed).
- Using Many-To-Many Field in Django
- Building forms for models
- Retrieving book details via Amazon REST
That is the progress that I’ve made so far. I want the below features built so that it is useful.
- Ajax Integration
- Barcode Integration
I’m planning to use jQuery for Ajax. I still need to find an ‘open source’ Barcode reader. I don’t want to buy a Barcode reader; I want to use the webcam or the camera attached with the mobile. If you’ve any suggestions, please let me know.
I’m sure if it all works out well, then I’ll build a book inventory easily and keep it updated too.
If you are interested, check out the code at Google Code.
Pagination in Django
Most often it is necessary to paginate the results. Django comes up with a simple but pretty neat pagination out-of-the-box. Paginating is easy and simple.
Import first:
from django.core.paginator import Paginator, InvalidPage
Get the results. I read somewhere that Django delays execution of queries until necessary. If so, then this will not impact any performance.
info_list = Paginator(sol.objects.all(),paginate_by)
Now build the dictionary values to be passed to the template.
try:
page_info = info_list.page(page_num)
except InvalidPage:
page_num = 1
page_info = info_list.page(page_num)
has_previous = page_info.has_previous()
has_next = page_info.has_next()
info_dict = {
'query_list' : page_info.object_list,
'has_previous' : page_info.has_previous(),
'previous_page' : page_info.previous_page_number(),
'has_next' : page_info.has_next(),
'next_page' : page_info.next_page_number(),
'site_name' : 'sol',
'user' : request.user,
}
That is it. Now this can be called as:
{% for item in query_list %}
<tr class="itemrow">
<td class="itemcol"><a href="{{ item.get_absolute_url }}"> {{item.title}}</a></td>
<td class="itemcol">
<ul>
{% for author in item.authors.all %}
<li> <a href="{{author.get_absolute_url}}" title="{{author.name}}">{{author.name}}</a> </li>
{%endfor %}
</ul>
</td>
</tr>
{% endfor %}
This will enable pagination for the entries. Simple right?
First Experience
I am not proud of saying this. Yet the truth is that, I never voted in Indian elections.
It is not that I am one of the many many frustrated Indians about Indian Democracy. It is true that most of our politicians have been detrimental to country’s progress rather than contributing to it. Despite that sad fact, democracy is one of the greatest strengths of India.
The reason I never voted is related to me being in IT industry. I was born in Tuticorin, a city on the south end of India. However I studied in Coimbatore and then worked in Chennai. When I came of age to vote, I didn’t care about voting and elections. Once I got to realize that I should be a responsible citizen, I moved out of the country. As India doesn’t have a e-voting mechanism, I couldn’t exercise my rights.
Another constraint in voting is that one need to be in the registered constituency. With a job that demands a heavy migration, I couldn’t vote until now.
But this time, I was determined to vote. I should say that my Dad was determined that all of us vote. He ensured that all of us got registered in the proper constituency and got us the voter ids. But we had fun (!) looking through our names – My Dad’s name is Justin Rosary John Britto and I am Joseph Xavier Jude; our names were printed as vasa jastina rojari jana brote and josepha jeviyara judisa. Same was true for the entire family.
We went to the polling booth immediately after lunch. We expected that there will be huge crowd. There wasn’t. We were the only ones at that time. We had already discussed about whom to vote. So we voted quietly and returned home.
I am proud to be a responsible citizen.
Dilemma
It happened a little more than a year ago. Yet it looks so relevant today.
The objective of the engagement was to develop a comprehensive project management tool for the organization. Ironically, none of the project management processes were followed in developing the tool. Every mistake that could be committed in a project was committed – scope creep, loosing focus, failure to plan, failure to execute; you name it and it was there. As a result, the project was getting nowhere and not surprisingly top management was furious.
Having spent close to 30 man year’s of effort, scrapping the project became out-of-question. Something (anything) had to be done to bring life back into project so as to avoid embarrassment. Few project managers had quit and due to its state, no one else was willing to pitch in. Though I’m from ERP world and the tool was developed in J2EE, I offered to try my chance. (Sastri who taught me project management on a practical manner, always encouraged to take risks; In fact I witnessed him completing a project that was abandoned by other project managers)
In six months, the project was back on track; stable; today it is the default project management tool in the company. I didn’t do anything extra-ordinary – I just went by the book.
People, People, People
You need two strong oxen to plough a field; not hundred chickens
First thing I did was to re-organize the team. Lots of folks were not enthusiastic to work on the project anymore. I didn’t blame them. I called up a team meeting and said that, ‘I’ll bring stability for the project and we’ll be a success; however if anyone want to leave I am okay with it. But leave now’. Only few people stayed back. In terms of skills, not all of them were strong oxen; but in terms of commitment, they were the best.
It was not just the team that was behind the success; I met up with the senior management and solicited their support and co-operation. I got it – my supervisor; head of department, and all other senior managers were so supportive of my efforts.
If I were to contribute the success to one factor, it would be people.
Lean Process
Once I got the right people, I went on to define the process that we will follow – from gathering requirements to release. Considering the fire-fighting situation, I kept it as lean as possible.
As we progressed, we reviewed the processes and tailored to suit our situation.
Having a lean process helped as it was easy for the team to understand and to follow.
Determined Execution
What sets a leader apart is not those brilliant ideas; but the realization of those ideas.
Once I got the people that I can depend on and a process framework, I went about getting the project on track. Every day I had a stand-up status meeting with the team; I met with the stakeholders regularly; Luckily I got few enthusiastic champions for the tool in various departments; more importantly I was firm on the timelines and what got into each release. In few months, the trust was built and then it was easy to build on top of that trust.
Six months were insane. Very insane. There were days we were in the office for 36 hours(!). Unbelievable; but true. (I stayed that 36 hours too; as I believe I shouldn’t ask the team to do something, if I myself am unwilling to do so).
Once we passed the insane days, it was smooth and in fact superb. The team was congratulated in every relevant forum/meeting. We grew in our confidence as well. It all worked out pretty fine.
The Dilemma
I was back to ERP projects and it all seemed settled. Until now.
Apparently there are enough projects that go through a disastrous phase. There is another critical project in such a phase (why do critical projects slips through the gaps in project management). With the past experience, it is falling into my lap.
Honestly speaking, I miss those days – the tension, the stress and everything associated with stabilizing the project. On one hand, I’m not so sure if I could go through another period of insanity. On the another hand, the very thought of taking risk excites me.
I am in a dilemma.