Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#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

https://github.com/gotling/bug-django2-autocomplete

Attachments (1)

screenshot (24.6 KB ) - added by Marcus Götling 6 years ago.
Inline

Download all attachments as: .zip

Change History (12)

by Marcus Götling, 6 years ago

Attachment: screenshot added

Inline

comment:1 by Claude Paroz, 6 years ago

Triage Stage: UnreviewedAccepted

I was able to reproduce, but only for new forms added by clicking on the "Add another ..." link.

comment:2 by Tim Graham, 6 years ago

Cc: Johannes Maron added
Severity: NormalRelease blocker

comment:3 by Johannes Maron, 6 years ago

Owner: changed from nobody to Johannes Maron
Status: newassigned

Might I just say: This is an amazing bug report! I'll look into it this weekend, thanks :)

comment:4 by Tim Graham, 6 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 Tim Graham, 6 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  
    2929
    3030    $(document).on('formset:added', (function() {
    3131        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()
    3633        };
    3734    })(this));
    3835}(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 Tim Graham, 6 years ago

Owner: changed from Johannes Maron to Tim Graham

I think a have a working patch and am writing a test.

comment:7 by Tim Graham, 6 years ago

Has patch: set

comment:8 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 81057645:

Fixed #28871 -- Fixed initialization of autocomplete widgets in "Add another" inlines.

Also allowed autocomplete widgets to work on AdminSites with a name other
than 'admin'.

comment:9 by Tim Graham <timograham@…>, 6 years ago

In 63d425c6:

[2.0.x] Fixed #28871 -- Fixed initialization of autocomplete widgets in "Add another" inlines.

Also allowed autocomplete widgets to work on AdminSites with a name other
than 'admin'.

Backport of 81057645f61fe545f4f11737dbd3040043ed2436 from master

comment:10 by Tim Graham <timograham@…>, 6 years ago

In 7664fe2:

Refs #28871 -- Fixed admin_views selenium test failure.

comment:11 by Tim Graham <timograham@…>, 6 years ago

In dbdf5dea:

[2.0.x] Refs #28871 -- Fixed admin_views selenium test failure.

Backport of 7664fe275910bb31fcca2d54844bedde19fc4ed9 from master

Note: See TracTickets for help on using tickets.
Back to Top