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 http://stackoverflow.com/questions/291945/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