开发者

Django-selectable with dynamic inlines

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 check开发者_运维知识库ing 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 ?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜