Django access data passed to form

Posted by realshadow on Stack Overflow See other posts from Stack Overflow or by realshadow
Published on 2010-06-16T15:23:17Z Indexed on 2010/06/16 16:22 UTC
Read the original article Hit count: 387

Hey,

I have got a choiceField in my form, where I display filtered data. To filter the data I need two arguments. The first one is not a problem, because I can take it directly from an object, but the second one is dynamically generated. Here is some code:

class GroupAdd(forms.Form):
    def __init__(self, *args, **kwargs):
        self.pid = kwargs.pop('parent_id', None)

        super(GroupAdd, self).__init__(*args, **kwargs)

    parent_id = forms.IntegerField(widget=forms.HiddenInput)
    choices = forms.ChoiceField(
        choices = [
            [group.node_id, group.name] for group in Objtree.objects.filter(
                 type_id = ObjtreeTypes.objects.values_list('type_id').filter(name = 'group'), 
                 parent_id = 50
            ).distinct()] + [[0, 'Add a new one']
        ], 
        widget = forms.Select(
            attrs = {
                'id': 'group_select'
            }
        )
     )

I would like to change the parent_id that is passed into the Objtree.objects.filter. As you can see I tried in the init function, as well with kwargs['initial']['parent_id'] and then calling it with self, but that doesnt work, since its out of scope... it was pretty much my last effort. I need to acccess it either trough the initial parameter or directly trough parent_id field, since it already holds its value (passed trough initial).

Any help is appreciated, as I am running out of ideas.

© Stack Overflow or respective owner

Related posts about python

Related posts about django