Pensieri di un lunatico minore

7 December 2007 Python

Django forms and annoyances

So, I’m working on a project. I have no idea if I’ll be allowed to open source it or not, but that’s neither here nor there for now. I’ve been using this as a good project to work on for learning Django to add to my tool-belt of web frameworks. Certainly the Pythonesque aspects have been easy, as I’ve been writing Python code since 1995. There is one thing that bugs me, though, and that is the way forms work. Let me first show you the code:

issue = Issue()
if request.method == 'POST':
    form = IssueForm(issue, request.POST)
    if form.is_valid():
        issue.creator = request.user
        issue.status = Status.objects.get(name__exact='New')
        issue.owner = Component.objects.get(id__exact=form.data['component']).lead
        new_issue = form.save()
else:
    form = IssueForm(issue)
return render_to_response('issue/new.html', {'form': form},
    context_instance=RequestContext(request))

Now, I’m sure this is suboptimal code, but I’ve yet to figure out a clean way to only have some of my data collected to a form, and properly populate the object with the rest of the data in the background. Is this how you do it? It works, sure, but something says “kludge” to me. It’s not ugly per say, but it just feels less than elegant.

Maybe I’m just missing something?

This entry was posted at 11:32 pm on 7 December 2007 and is filed under Python. You can follow any responses to this entry through the post-specific RSS 2.0 feed.

Hey there,

What do you find kludgy about it—the fact that you’re assigning issue.creator, issue.status and issue.owner? It seems quite clean and straightforward to me…

Adrian

I don’t know if I can put my finger on it. It’s just a “feeling”, and I know how useful those are to developers. If I can figure out one that makes me feel better, I’ll mention it, I suppose.

Both comments and pings are currently closed.