Django: Filtering a ModelForm field
So something what should be so relatively easy can turn out to be quite hard when you don’t know where to go for the answer. Generally the #django channel on IRC is an awesome help, but this morning I felt invisible so I can too keep digging. Finally I found my answer in code from another project.
Here’s the situation. I’m creating a budget system. When you add or import transactions from your bank, you need to tell it what bank account the transactions are associated with. I have the following models: Bank, BankAccount, Transaction. Transaction has a foreign key to BankAccount.
I have a ModelForm name AddTransactionForm. The problem was that when I went to add a transaction, it would show me all the accounts of other users. I needed to somehow filter the queryset that was applied to the account foreign key. I couldn’t find this anywhere in the docs, and like I mentioned, no one was even acknowledging me in IRC, which is rare. I finally found something similar I did for HouseSheet.com. Here was is my “fixed” form class:
class AddTransactionForm(ModelForm):
def __init__(self, user, *args, **kwargs):
super(AddTransactionForm, self).__init__(*args, **kwargs)
self.fields['account'] = forms.ModelChoiceField(queryset=BankAccount.objects.filter(user=user))
class Meta:
model = Transaction
exclude = ('envelope',)
So now, when I call this, I just need to pass in the the logged in user to show only that user’s accounts like so:
form = ImportForm(user=request.user)
I think my biggest problem with this was I haven’t yet mastered the python language so I wasn’t sure how to pass in the user object I need to filter on.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
March 10, 2009
·
Dustin ·
13 Comments
Tags: django · Posted in: Programming & Internet
-
Fernando
-
farout
-
farout
-
http://www.propeller.com/member/timelessrides/ Daniel
-
http://www.inin.com whitey@Call Center Software
-
http://www.badcreditloancenter.com Daniel@Bad Credit Loans
-
http://leon-huang.com/ Leon Huang@Singapore Photographer
-
http://aviary.com/artists/gregorlily Stacy H
-
http://www.thekellerboard.com Keller Tx
-
http://www.herkules.us/lifts Jenny@ Lift Tables
-
http://texas-car-insurances.blogspot.com/ Mustana@Texas Car Insurance
-
http://www.healthclubhut.com/ John Johnson


