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 7831697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:50:43+00:00 2026-06-02T11:50:43+00:00

I want to filter ManyToManyField choices in my ModelForm: class MyForm(forms.ModelForm): class Meta: model

  • 0

I want to filter ManyToManyField choices in my ModelForm:

class MyForm(forms.ModelForm):
    class Meta:
        model = Entity
        fields = ['parent_entities']

    def __init__(self, *args, **kwargs):
        self.root_entity = kwargs.pop('root_entity')
        self.Meta.fields['parent_entities'].queryset = Entity.objects.filter(root_entity=self.root_entity)
        super(MyForm, self).__init__(*args, **kwargs)

I tried a lot of different code I’ve seen but nothing has worked yet.

I guess my problem is that I can’t get this ‘parent_entities’ field.
With this code, I have the error :

list indices must be integers, not str
  • 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-06-02T11:50:44+00:00Added an answer on June 2, 2026 at 11:50 am
    def __init__(self, *args, **kwargs):
       # First pop your kwargs that may bother the parent __init__ method
       self.root_entity = kwargs.pop('root_entity')
       # Then, let the ModelForm initialize:
       super(MyForm, self).__init__(*args, **kwargs)
       # Finally, access the fields dict that was created by the super().__init__ call
       self.fields['parent_entities'].queryset = Entity.objects.filter(root_entity=self.root_entity)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Item(models.Model): ... class ItemSet(models.Model): items = models.ManyToManyField(Item, related_name=itemsets) I have a list of
I have a four classes: class A(models.Model): field1 = models.BooleanField(default = True) class Meta:
I want to filter a class by providing a list first_name = ['John', 'Lian']
I have a model with a m2m relation to users: class SomeModel(models.Model): user=models.ManyToManyField(User,related_name='linked',blank=True,null=True) to
class Message(models.Model): date = models.DateField() user = models.ForeignKey(UserEx) groups = models.ManyToManyField(Group) main_category = models.ForeignKey(MainCategories)
I have this structure of model objects: Class A: b = models.ManyToManyField(B) Class B:
I have the models: class Article(models.Model): title = models.TextField(blank=True) keywords = models.ManyToManyField(Keyword, null=True, blank=True)
I have a simple model like this class UserType( models.Model ) : def __unicode__(
I have two models: class Production(models.Model): gallery = models.ManyToManyField(Gallery) class Gallery(models.Model): name = models.CharField()
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):

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.