Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 854443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:55:27+00:00 2026-05-15T07:55:27+00:00

I have got a choiceField in my form, where I display filtered data. To

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:55:27+00:00Added an answer on May 15, 2026 at 7:55 am

    OK a couple of minor points here before I answer your question.

    Firstly, your field should probably be a ModelChoiceField – this takes a queryset parameter, rather than a list of choices, which avoids the need for the list comprehension to get id and value.

    Secondly, your query to get the Objtree objects is much better written using the double-underscore notation to traverse relations:

    Objtree.objects.filter(type__name='group', parent_id=50)
    

    Now, the actual question. As you note, you can’t access local or instance variables within the field declarations. These are class-level attributes, which are processed (via the metaclass) when the class is defined, not when it is instantiated. So you need to do the whole thing in __init__.

    Like this:

    class GroupAdd(forms.Form):
        parent_id = forms.IntegerField(widget=forms.HiddenInput)
        choices = forms.ModelChoiceField(queryset=None)
    
        def __init__(self, *args, **kwargs):
            pid = kwargs.pop('parent_id', None)
            super(GroupAdd, self).__init__(*args, **kwargs)
            self.fields['choices'].queryset = Objtree.objects.filter(
                                                  type__name='group', parent_id=pid
                                              )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got two movieclips on the main timeline. First one is envelope_mc and
I have got a bit of a problem. I have two pages (Thumbnail.aspx &
i have got <div><a class='link' href='index.php' data-container='target'>Load</a></div> <div class='target'></div> i need to write jquery
I have got a problem on casting an object to one of it's base
I have got one app running on two servers, one works and one doesnt
I have got relative layout, in that i have placed two textviews, one image
I have got two comboboxes on my application. The first combobox can get all
I have got following folder structure for my domain (domain.com): /data/images (it´s subdomain name,
I've got a view with a few different forms in it. The first form
I have got my HTML5 project a bit further along - I have one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.