Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct field to use in Admin form when multi=True #388

Open
philgyford opened this issue Jun 15, 2022 · 1 comment
Open

Correct field to use in Admin form when multi=True #388

philgyford opened this issue Jun 15, 2022 · 1 comment

Comments

@philgyford
Copy link
Contributor

Although there's stuff in the README about custom forms and the custom widgets, I couldn't see anything about what to do if you're using multi=True. I've got it working, but I'm not sure if this is the best way.

I have this model:

from django.db import models
from django_countries.fields import CountryField

class Person(models.Model):
    countries = CountryField(multiple=True, blank=True)

And then in admin.py:

from django import forms
from django.contrib.admin.widgets import FilteredSelectMultiple
from django_countries import countries as countries_object
from .models import Person

class PersonForm(forms.ModelForm):
    class Meta:
        model = Person
        exclude = ()

    countries = forms.MultipleChoiceField(
        choices=list(countries_object),
        widget=FilteredSelectMultiple("countries", is_stacked=False),
        required=False,
    )

The choices is the bit I'm least sure about.

If it is the best way then maybe something could be added to the README to help the next person who's confused?

@craigRSA
Copy link

craigRSA commented Jun 27, 2023

dylan-tkt provides a different solution in #273 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants