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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:47:24+00:00 2026-05-12T00:47:24+00:00

Alright, I’m having a hard time explaining this, let me know if I should

  • 0

Alright, I’m having a hard time explaining this, let me know if I should fill you in on more details.

My url looks like this: http://domain.com/<category>/
Each <category> may have one or many sub categories.

I want the category page to have a form with a select box (among other fields) containing the category sub-categories.
I’ve currently hard coded the form in one of the templates, but I want to make it reflect the model directly.

In my currently hard coded solution, I have in my category view:

s = Category.objects.filter(parents__exact=c.id)  

that the form template iterates through and prints out the select box (see model code below)

I’m guessing I want a ModelFormSet with an init that filters out the categories, but I can’t seem to find how to do it in the docs.

Been looking at How do I filter ForeignKey choices in a Django ModelForm? as well, but I can’t get it to work properly.

My models

# The model that the Form should implement
class Incoming(models.Model):
    cat_id = models.ForeignKey(Category)
    zipcode = models.PositiveIntegerField()
    name = models.CharField(max_length=200)
    email = models.EmailField()
    telephone = models.CharField(max_length=18)
    submit_date = models.DateTimeField(auto_now_add=True)
    approved = models.BooleanField(default=False)

# The categories, each category can have none or many parent categories
class Category(models.Model):
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField()
    parents = models.ManyToManyField('self',symmetrical=False, blank=True, null=True)

    def __unicode__(self):
        return self.name

My form

class IncomingForm(ModelForm):
    class Meta:
        model = Incoming
  • 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-12T00:47:24+00:00Added an answer on May 12, 2026 at 12:47 am

    I would edit Daniel Rosemans reply and vote it winner, but since i cant edit it i will post the correct answer here:

    class IncomingForm(ModelForm):
        class Meta:
            model = Incoming
    
        def __init__(self, *args, **kwargs):
            super(IncomingForm, self).__init__(*args, **kwargs)
            if self.instance:
                self.fields['cat_id'].queryset = Category.objects.filter(
                        parents__exact=self.instance.id)
    

    The difference is self.fields[‘cat_id’] (correct) vs self.fields[‘parents’] (wrong, we both made the same mistake)

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

Sidebar

Related Questions

Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
Alright, I don't know if anyone has tried to do this yet, however. I
Alright. So I figure it's about time I get into unit testing, since everyone's
Alright. So I have a very large amount of binary data (let's say, 10GB)
Alright, I know how the fieldset / legend works out in HTML. Say you
Alright, quick question. A server is running in Eastern Time. PHP program needs to
Alright I'm a bit baffled by this one. Changing an unrelated int property on
Alright, I guess this question has been asked a lot of times here. I
Alright, Im going about this, with what is probably a really complicated solution, but

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.