Last active
November 22, 2023 11:25
-
-
Save jdklub/9261959 to your computer and use it in GitHub Desktop.
Edit a many-to-many relationship (ManyToManyField) on the Django Admin change list page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BookChangeList(ChangeList): | |
| def __init__(self, request, model, list_display, list_display_links, | |
| list_filter, date_hierarchy, search_fields, list_select_related, | |
| list_per_page, list_max_show_all, list_editable, model_admin): | |
| super(BookChangeList, self).__init__(request, model, list_display, list_display_links, | |
| list_filter, date_hierarchy, search_fields, list_select_related, | |
| list_per_page, list_max_show_all, list_editable, model_admin) | |
| # these need to be defined here, and not in BookAdmin | |
| self.list_display = ('name', 'categories') | |
| self.list_display_links = ['name'] | |
| self.list_editable = ['categories'] | |
| class BookForm(forms.ModelForm): | |
| # this the bit of custom CSS we want to add | |
| style_text = "height:80px; overflow-y:scroll;" | |
| # here we only need to define the field we want to be editable | |
| categories = forms.ModelMultipleChoiceField(queryset=BookCategory.objects.all(),widget=StyleableCheckboxSelectMultiple(ul_attrs={"style":style_text}),required=False) | |
| class BookAdmin(admin.ModelAdmin): | |
| def get_changelist(self, request, **kwargs): | |
| return BookChangeList | |
| def get_changelist_form(self, request, **kwargs): | |
| return BookForm | |
| admin.site.register(Book, BookAdmin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # this subclass allows us to add a bit of our own CSS at the <ul> level | |
| class StyleableCheckboxSelectMultiple(forms.CheckboxSelectMultiple): | |
| def __init__(self, attrs=None, ul_attrs=None): | |
| self.ul_attrs = ul_attrs | |
| super(ScrollableCheckboxSelectMultiple, self).__init__(attrs) | |
| def render(self, name, value, attrs=None, choices=()): | |
| html = super(ScrollableCheckboxSelectMultiple, self).render(name, value, attrs, choices) | |
| final_attrs = self.build_attrs(self.ul_attrs) | |
| return mark_safe(html.replace('<ul>','<ul%s>' % flatatt(final_attrs))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BookCategory(models.Model): | |
| name = models.CharField(max_length=255) | |
| class Book(models.Model): | |
| name = models.CharField(max_length=255) | |
| categories = models.ManyToManyField('BookCategory', blank=True, null=True) | |
| def __unicode__(self): | |
| return self.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.