1

I'm using django-selectable ( https://bitbucket.org/mlavin/django-selectable ) with an admin tabularInline to get autocomplete functionality on one of the inline fields. It works for inlines added at creation time. The problem I'm having is that the autocomplete functionality isn't added when the user adds another row to the inline.

There's a bug and fix for this issue here

https://bitbucket.org/mlavin/django-selectable/issue/12/make-it-work-with-dynamically-added-forms And looking at jquery.dj.selectable.js near the bottom is :

if (typeof(django) != "undefined" && typeof(django.jQuery) != "undefined") {
    if (django.jQuery.fn.formset) {
        var oldformset = django.jQuery.fn.formset;
        django.jQuery.fn.formset = function(opts) {
            var options = $.extend({}, opts);
            var addedevent = function(row) {
                bindSelectables($(row));
            };
            var added = null;
            if (options.added) {
                var oldadded = options.added;
                added = function(row) { oldadded(row); addedevent(row); };
            }
            options.added = added || addedevent;
            return oldformset.call(this, options);
        };
   }
}

It looks like this should make the autocomplete work with dynamically added rows, but I can't work out what to do for this to work. The admin tabularInline.html has inline_admin_formset so should I be checking for that and not django.jQuery.fn.formset as in the code above ? Or somehow adding inline_admin_formset to django.jQuery.fn ?

Thanks very much for any suggestions.


I'm using version 0.2. In forms.py there is the inline form :

    class GrammarInlineForm(forms.ModelForm):
        class Meta:
            model = Grammar
            widgets = {
            'description' :forms.Textarea(attrs={'cols': 80, 'rows': 10, 'class': 'grammarInline'}),
            'title' : selectable.AutoCompleteSelectWidget(lookup_class=GrammarLookup, allow_new=True),
        }   
        exclude = ('creation_date', 'creator', 'plan')

        def __init__(self, *args, **kwargs):
        super(GrammarInlineForm, self).__init__(*args, **kwargs)

In admin.py the inline admin is made and added to the main admin ( PlanAdmin ) :

    class GrammarInline(admin.TabularInline):
        form = GrammarInlineForm
        model = Grammar
        extra = 2

        def save_formset(self, request,form, formset, change):
            instances = formset.save(commit=False)
            for instance in instances:
                instance.creator = request.user
                instance.save()
            formset.save_m2m()

    class PlanAdmin(admin.ModelAdmin):
        form = PlanForm
        list_display = ('title', 'topic', 'level', 'description','public', )
        inlines = [ ActivityInline, GrammarInline, ]

After reading your ticket http://code.djangoproject.com/ticket/15760 I tried binding to the inlines formsetadd event, like this

    django.jQuery('.ui-autocomplete-input').live('formsetadd', function(e, row) {
        console.log('Formset add!');
        console.log($(row));
       });

but looking at django/contrib/admin/media/js/inlines.js it seems that these triggers aren't in version 1.3.1 of django. Is it necessary to bind to an event that gets triggered when an inline is added? There is a similar case here https://bitbucket.org/mlavin/django-selectable/issue/31/dynamically-added-forms but that's using the formset plugin. Is there a way to use bindSelectable(row) to the admin inline ?

4

1 回答 1

0

您发布的jquery.dj.selectable.js代码可以在添加新行时进行修补django/contrib/admin/media/js/inlines.js调用。http://code.djangoproject.com/ticket/15760已打开,因此这个猴子补丁不是必需的,但尚未关闭,对于 Django 1.4 可能不会关闭。同样,您不需要做任何事情来完成这项工作。您无需更改模板。您不需要编写任何额外的 JS。bindSelectable(row)

项目源有一个使用动态表格内联的工作示例:https ://bitbucket.org/mlavin/django-selectable/src/33e4e93b3fb3/example/core/admin.py#cl-39

于 2011-11-16T14:30:41.870 回答