#28871 closed Bug (fixed)
Autocomplete select for new lines in inline model do not open
Reported by: | Marcus Götling | Owned by: | Tim Graham |
---|---|---|---|
Component: | contrib.admin | Version: | 2.0 |
Severity: | Release blocker | Keywords: | autocomplete select2 |
Cc: | Johannes Maron | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | yes |
Description
When a model has an Inline model, autocomplete select does not show for new rows.
Select box is is replaced with Select2 style, but clicking it does not show the select list.
model.py
class Item(models.Model): name = models.CharField(max_length=100) class Category(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name class LineItem(models.Model): item = models.ForeignKey( 'Item', on_delete=models.CASCADE ) category = models.ForeignKey( 'Category', on_delete=models.CASCADE )
admin.py
class LineItemInline(admin.TabularInline): model = LineItem extra = 1 autocomplete_fields = ('category',) @admin.register(Item) class ItemAdmin(admin.ModelAdmin): list_display = ('name',) search_fields = ('name',) inlines = [ LineItemInline ] @admin.register(Category) class CategoryAdmin(admin.ModelAdmin): list_display = ('name',) search_fields = ('name',)
Github repo
With demo and instructions on how to reproduce
Attachments (1)
Change History (12)
by , 7 years ago
Attachment: | screenshot added |
---|
comment:1 by , 7 years ago
Triage Stage: | Unreviewed → Accepted |
---|
I was able to reproduce, but only for new forms added by clicking on the "Add another ..." link.
comment:2 by , 7 years ago
Cc: | added |
---|---|
Severity: | Normal → Release blocker |
comment:3 by , 7 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Might I just say: This is an amazing bug report! I'll look into it this weekend, thanks :)
comment:4 by , 7 years ago
The Django 2.0 release is scheduled for today so maybe me or somebody else would look at the issue if you don't have time today.
comment:5 by , 7 years ago
I found that this patch will allow the "Add another" autocomplete widgets to work, except for the first one that's added.
-
django/contrib/admin/static/admin/js/autocomplete.js
diff --git a/django/contrib/admin/static/admin/js/autocomplete.js b/django/contr index 15321f9..2fa1920 100644
a b 29 29 30 30 $(document).on('formset:added', (function() { 31 31 return function(event, $newFormset) { 32 var $widget = $newFormset.find('.admin-autocomplete'); 33 // Exclude already initialized Select2 inputs. 34 $widget = $widget.not('.select2-hidden-accessible'); 35 return init($widget); 32 return $newFormset.find('.admin-autocomplete').djangoAdminSelect2() 36 33 }; 37 34 })(this)); 38 35 }(django.jQuery));
It looks like the contents of $newFormset
for the first "add another" is different from the contents of subsequent "add anothers".
comment:6 by , 7 years ago
Owner: | changed from | to
---|
I think a have a working patch and am writing a test.
Inline