Uncategorized
Times are changing
Jun 23rd
It has been almost a full year since my last post, in that short time much has changed. My previous place of work has decided to pull their offices out of town leaving myself and many others behind. Many sought refuge in a few larger businesses in town and seem to be enjoying it. A few have ventured out into smaller local companies, bringing with them there knowledge and expertise. In January I decided I would do something I have always wanted to do, I incorporated Nullobject and started working on contracts and whatever work I could find in an effort to build up some money into the company as well as keep my bills payed.
I have decided that I want to keep a record of some of the stuff I am working on, as well as continue to help people learn in all areas. I plan on posting articles on things as I did before, problems I tackle and solve, solution to problems people have, as well as the usual rants and opinions. I am hoping that I will learn more by teaching others and others will learn as well. So anyone that can bare my horrible writing and want to learn new stuff together, are welcome to join me. I plan on keeping updates frequent and with substance.
Django ModelForms With Foreign Keys
Feb 14th
I have recently been working more and more with django forms. In previous projects we decided not to use them mostly cause we couldn’t get them to do what we wanted them to do. In retrospect it was more of a lack of understanding how they worked and insufficient time to sit down and learn them.
Earlier in the week I was working with a model based form with a foreign key to another model. Problem I ran across was that I wanted said form to be displayed on the page, and also post the proper foreign key id without the user selecting it ( foreign keys are selected through a select box which can get quite large ). The page in which this form would be displayed would always know the foreign key id, so there would never need to be a situation where the form would ever need to show the user a select box.
After much trial and error, here is what I came up with.
from django.forms import ModelForm
from django import forms
from models import Thread
from models import Post
class ThreadForm(ModelForm):
class Meta:
model = Thread
fields = ('Message', 'user')
class PostForm(ModelForm):
class Meta:
model = Post
fields = ( 'Message', 'user')
The Post in question would normally have a foreign key in this instance to Thread, but we exclude it from the form so that it is not validated against or displayed to the user.
<form action="/post/{{ thread.pk }}/" method="post">
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
In the actual form posting we are using the threads key in the url that is receiving the new post, If you were not using in the url, you could simple just add it as a hidden value on the page.
def post_add(request, thread_id):
a_form = PostForm(request.POST)
if(a_form.is_valid()):
post = a_form.save(commit=False)
post.thread_id = thread_id
post.save()
In the post_add view which accepts the thread_id and passes it to the method, we create the form based on the post data. validate it and save it. By adding the commit=False we stop the Form from saving the data to the database. and we return a model object. Which we can then pass along the thread_id and save it.
Their are a few things that could be added to the view for extra checking. We could do a lookup for a thread id and make sure its there, and do checks for request.method, or even add in handling for ajax requests. The example view isn’t complete and is only there as a quick sample of how to handle these types of forms.
A New Beginning
Feb 8th
A new beginning, a site refreshed and ready to be filled with an abundance of ideas. I plan on using this site as a place to store and share my ideas. Share code I have either found or wrote myself; and hopefully discuss with others solutions to problems that are encountered on the way.
A vast majority of the code you will find on this site will be expressed in what ever languages I am currently dealing with either at home or at work. Currently I have been working mostly in Python and C#. But I have been known to jump into Java, PHP, C/C++, and on those dreadful days Coldfusion. Since I have been migrating my home environment from Windows to a more Linux and OSX system, there is a good chance I’ll be posting some experience from that as well.
My plan for this site is to get into a good groovy and have a decent amount of posts going each week. I also intend on interacting in the comments frequently and help people that request it.
