Formset inside Formset

26 views
Skip to first unread message

rgreene

unread,
May 14, 2014, 11:09:41 AM5/14/14
to django...@googlegroups.com
Good day,

We have an inline formset where each instance of the form has a ManyToManyField for selecting countries. Here is the code:

# models
class Geographic_Scope_Region(models.Model):
    # class name exception (underscore) because ManyToManyField assumes pk is lower(Table_Name) + _id
    geographic_scope_region_id = models.AutoField(primary_key=True)
    issf_core = models.ForeignKey(Issf_Core)
    REGION_NAME = (
        ('Asia', 'Asia'),
        ('Africa', 'Africa'),
        ('Caribbean', 'Caribbean'),
        ('Europe', 'Europe'),
        ('Latin America', 'Latin America'),
        ('North America', 'North America'),
        ('Oceania', 'Oceania'),
        ('Other', 'Other'),
    )
    region_name = models.CharField(choices=REGION_NAME, max_length=100)
    region_name_other = models.CharField(max_length=100)
    countries = models.ManyToManyField(Country, db_table='geographic_scope_region_country', blank=True)

    class Meta:
        managed = False
        db_table = 'geographic_scope_region'

# forms
class GeographicScopeRegionForm(ModelForm):
    class Meta:
        model = Geographic_Scope_Region
        labels = {
            'region_name': 'Name of region',
            'region_name_other': 'Other region name',
            'countries': 'Select country(ies) (if applicable)',
        }

GeographicScopeRegionInlineFormSet = inlineformset_factory(Issf_Core, Geographic_Scope_Region, form=GeographicScopeRegionForm, extra=2)

# views
class GeographicScopeRegionUpdateView(UpdateView):
    model = Issf_Core
    form_class = GeographicScopeRegionInlineFormSet
    success_url = '/'

It works great and produces the following (simplified) output:


By changing the widget from the default SelectMultiple to CheckboxSelectMultiple, the countries multi-select list becomes a set of checkboxes, one for each of ~200 countries!

We're wondering if there's an easy way to instead use an inline formset for the countries, so that each form instance is a Select widget for picking one country?

Thanks in advance,
Randal
Reply all
Reply to author
Forward
0 new messages