1

インラインフィールドの1つでオートコンプリート機能を取得するために、admin tabularInlineでdjango-selectable(https://bitbucket.org/mlavin/django-selectable )を使用しています。作成時に追加されたインラインで機能します。私が抱えている問題は、ユーザーがインラインに別の行を追加したときにオートコンプリート機能が追加されないことです。

ここにこの問題のバグと修正があります

https://bitbucket.org/mlavin/django-selectable/issue/12/make-it-work-with-dynamically-added-forms そして下部にあるjquery.dj.selectable.jsを見ると:

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);
        };
   }
}

これにより、動的に追加された行でオートコンプリートが機能するように見えますが、これを機能させるために何をすべきかがわかりません。admin tabularInline.htmlにはinline_admin_formsetがあるので、上記のコードのようにdjango.jQuery.fn.formsetではなく、それをチェックする必要がありますか?または、どういうわけかinline_admin_formsetをdjango.jQuery.fnに追加しますか?

提案をありがとうございました。


バージョン0.2を使用しています。forms.pyにはインラインフォームがあります:

    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)

admin.pyで、インライン管理者が作成され、メイン管理者(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, ]

チケットを読んだ後http://code.djangoproject.com/ticket/15760このようにインラインformsetaddイベントにバインドしようとしました

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

しかし、django / contrib / admin / media / js / inlines.jsを見ると、これらのトリガーはdjangoのバージョン1.3.1にはないようです。インラインが追加されたときにトリガーされるイベントにバインドする必要がありますか?https://bitbucket.org/mlavin/django-selectable/issue/31/dynamically-added-formsにも同様のケースがあり ますが、これ はフォームセットプラグインを使用しています。インラインでadminにbindSelectable(row)を使用する方法はありますか?

4

1 に答える 1

0

The jquery.dj.selectable.js code you posted is there to patch django/contrib/admin/media/js/inlines.js to call bindSelectable(row) when a new row is added. http://code.djangoproject.com/ticket/15760 was opened so that this monkey patch isn't necessary but has not been closed and likely will not be closed for Django 1.4. Again you shouldn't need to do anything to make this work. You don't need to change the template. You don't need to write any additional JS.

The project source has a working example of using a dynamic tabular inline: https://bitbucket.org/mlavin/django-selectable/src/33e4e93b3fb3/example/core/admin.py#cl-39

于 2011-11-16T14:30:41.870 に答える