tags:

views:

41

answers:

1

I have in my models Item with a many-to-many connection with Categories, and Categories have a Foreign Key to User.

What I'm hitting a road block figuring out is how to create a view with the intent to import an Item object to one or more of a User's Categories.

In it's most basic implementation I would like the view to display only the list of Categories that the User owns, and have the view process the form so that the Item is added to the appropriate Categories.


I've been struggling trying to figure out how to start this, including how to pass a User's categories to the form.

Thanks.

+1  A: 

in the form class (ItemForm) do this

def __init__(self,user,*args,**kwargs):
    super(ItemForm,self).__init__(*args,**kwargs)
    self.fields['categories'] = forms.ModelMultipleChoiceField(
        queryset=Categories.objects.filter(user=user))

then in your view call the form with :

form = ItemForm(request.user)

or

form = ItemForm(request.user, request.POST)

this should get you started. hopefully you can work out what you need to do from there. your question didn't leave much else to go by.

Brandon H
Boom. Worked. Thanks. I tried to stay sparse on the details just so that main roadblock would be answered and not to muddle it up with specifics. Thanks.
Malcolm Bastien