-
Notifications
You must be signed in to change notification settings - Fork 293
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
Admin form widget for multiple countries #273
Comments
example code in case anyone just wants to copy-paste workaround. from django import forms
from django.contrib.admin.widgets import FilteredSelectMultiple
class MovieForm(forms.ModelForm):
""" Use FilteredSelectMultiple instead for easier country selection. """
class Meta:
model = models.Movie
fields = '__all__'
widgets = {'countries': FilteredSelectMultiple('pays', is_stacked=False)} |
Thanks for the example @dylan-tkt . Just a note for anyone else doing this, I had an issue with class NoFirstRepeatCountries(Countries):
# Repeating first causes an issue with FilteredSelectMultiple used in admin
first_repeat = False
....
countries = CountryField(multiple=True, countries=NoFirstRepeatCountries, blank=True) |
@browniebroke @dylan-tkt @k0nG , thanks for code snippet, it works for admin. I use Was wondering if you have any library recommendation for a multiple select field such as manytomany and multiple countries |
I think there's been a change in how Django renders these fields and so the filter fields must be wrapped in As an example of how it works for a simple ManyToManyField, let's say I have these models: from django.db import models
class Publication(models.Model):
title = models.CharField(max_length=30)
class Article(models.Model):
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication) And then in my from django import forms
from django.contrib import admin
from django.contrib.admin.widgets import FilteredSelectMultiple, RelatedFieldWidgetWrapper
from .models import Article, Publication
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
exclude = ()
publications = forms.ModelMultipleChoiceField(
queryset=Publication.objects.all(),
widget=RelatedFieldWidgetWrapper(
FilteredSelectMultiple("publications", is_stacked=False),
rel=Article._meta.get_field("publications").remote_field,
admin_site=admin.site,
can_add_related=True,
)
)
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
filter_horizontal = ("publications",)
form = ArticleForm This works fine, replicating how the form would look by default, if I didn't declare the But if I replace that declaration of publications = forms.ModelMultipleChoiceField(
queryset=Publication.objects.all(),
widget=FilteredSelectMultiple("publications", is_stacked=False)
) then we get a messed-up layout with the field's label on the right: So, that's how it should work. Back to django-countries. In my project I have been using this, for a from django_countries import countries as countries_object
class WebsiteForm(forms.ModelForm):
class Meta:
model = Website
exclude = ()
countries = forms.MultipleChoiceField(
choices=list(countries_object),
widget=FilteredSelectMultiple("countries", is_stacked=False),
required=False,
) That used to work but at some point started generating the second layout above, with the field's label on the right. If I try to wrap the widget in countries = forms.MultipleChoiceField(
choices=list(countries_object),
widget=RelatedFieldWidgetWrapper(
FilteredSelectMultiple("countries", is_stacked=False),
rel=Website._meta.get_field("countries").remote_field,
admin_site=admin.site,
),
required=False,
) Then I get this error:
So I guess something is unexpected about |
We are using this field with
multiple=True
on a couple of models, and the Admin widget is a simple select multiple. Would be nice to enable a nicer widget by default, ideally reusing the one from Django'sfilter_horizontal
option.It looks like adding the
FilteredSelectMultiple
widget to the field on the admin form works, but could it be used by default? If not, would be nice to mention it in the readme maybe?The text was updated successfully, but these errors were encountered: