Skip to content

Instantly share code, notes, and snippets.

@zbyte64
Created September 6, 2017 03:02
Show Gist options
  • Save zbyte64/faaf36e2b22e81c7bdba7b25ad2aa809 to your computer and use it in GitHub Desktop.
Save zbyte64/faaf36e2b22e81c7bdba7b25ad2aa809 to your computer and use it in GitHub Desktop.
Django select2 example
from django.contrib import admin
from .models import Blog
from .fields import UserSelectWidget
class BlogForm(forms.ModelForm):
class Meta:
model = Blog
exclude = []
widgets = {
'publisher': UserSelectWidget,
}
class BlogAdmin(admin.ModelAdmin):
form = BlogForm
admin.site.register(Blog, BlogAdmin)
from django.conf import settings
from django_select2.forms import ModelSelect2MultipleWidget, ModelSelect2Widget
from django.contrib.auth.models import User
class UserSelectWidget(ModelSelect2Widget):
model = User
search_fields = [
'username__icontains',
'email__icontains'
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment