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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:18:19+00:00 2026-05-18T21:18:19+00:00

I have a object (Book) with a many-to-many relation with another object (Category). ‘Category’

  • 0

I have a object (Book) with a many-to-many relation with another object (Category).

‘Category’ is used to render subcategories too. The diference between a category and a subcategory is that a category has ‘category_parent=None’, while a subcategory has a category as a parent.

--models.py--
class Category(models.Model):
    name = models.CharField(max_length=150, blank=True, null=True)
    category_parent = models.ForeignKey("Categoria", blank=True, null=True)

class Book(models.Model):
    title = models.CharField("Title",max_length=150)
    category = models.ManyToManyField("Category", blank=True, null=True)

I’d like to display a form with two ChoiceFields as a multiple select. One for categories where only categories are to be displayed, and the other one for subcategories of the previous selected category.

I don’t know how to filter subcategories available in the category selected above. Is that possible?

Till now I’ve only been able to filter subcategories excluding those which are categories (category_parent=None), but I couldn’t filter it by the category that is already selected.

--forms.py--
class CatSubcatForm(forms.Form):
    category = forms.ModelChoiceField(Category.objects.filter(category_parent=None))
    subcategory = forms.ModelChoiceField(Category.objects.exclude(category_parent = None))
  • 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-18T21:18:19+00:00Added an answer on May 18, 2026 at 9:18 pm

    you can use the form init method to populate the subcategories select with the corresponding categories:

    forms.py

    class CatSubcatForm(forms.Form):
        category = forms.ModelChoiceField(Category.objects.filter(category_parent=None))
        subcategory = forms.ModelChoiceField(queryset=Category.objects.none(), required=False)
    
        def __init__(self, parent_category_id, *args, **kwargs):
            super(CatSubcatForm, self).__init__(*args, **kwargs) 
            self.fields['subcategory'].queryset = Category.objects.filter(pk=parent_category_id)
    

    views.py

     cat_subcat_form = CatSubcatForm(parent_category_id)
    

    after the user selects the parent category, you have to pass the parent category id with an ajax request to the form init method and you will only get the corresponding subcategories

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen many parcelable examples so far, but for some reason I can't
Let's say I have an ActiveRecord model called Book that has a has_many association
In Core Data is there a way to have one instance of an object
I have a question in core data: there are 2 Entities in the project,
I have to port an algorithm from an Excel sheet to python code but
I am reading Scott Meyers Effective C++ book. It was mentioned that there are
I have the following code, and am having some problems with it. Any help
I have a Assignment question about Java Generics. The given class is Product.java. It
A very basic question, but after reading the Design Patterns: Elements of reusable OO

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.