user391986
user391986

Reputation: 30886

django form with select fields

I have the following models

class Group(models.Model):
    name = models.CharField(max_length=32, unique=True)

class Subgroup(models.Model):
    name = models.CharField(max_length=32, unique=True)
    group = models.ForeignKey(Group)

class Keywords(models.Model):
    name = models.CharField(max_length=32, unique=True)
    subgroup = models.ForeignKey(Subgroup)

For each Subgroup I need to manage a list of keywords. I'm trying to use django forms to automatically display a list (select box) where if I add or remove values to that list and then issue a form.save that it automatically updates the models and data. How exactly can I do this? Are my models designed properly to allow this?

Upvotes: 0

Views: 682

Answers (1)

szaman
szaman

Reputation: 6756

I think you can create form with MultipleChoiceField:

class MyForm(forms.Form):
    to_select = forms.MultipleChoiceField(widget=forms.CheckboxInput, choices=[])

In this case you have to override form`s save method.

Did you try to create model form for subgroup class?

class MyForm(forms.ModelForm):
    class Meta():
        model=Subgroup

Upvotes: 2

Related Questions