Well, it's hard for me to explain this, let me know if I write you more details.
My url is as follows: http://domain.com/ <category > /
Each <category> may have one or more subcategories.
I want the category page to have a form with a selection field (among other fields) containing subcategories of the category. Currently, I have hardcoded the form in one of the templates, but I want it to reflect the model directly.
In my hard coded solution, I have categories in my view:
s = Category.objects.filter(parents__exact=c.id)
so that the form template repeats and prints the selection field (see model code below)
I assume I want a ModelFormSet with init that filters out categories, but I cannot find how to do this in the docs.
Looked at How to filter ForeignKey options in Django ModelForm? but I can't get it to work correctly.
My models
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)
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
source
share