我使用Django可選(https://bitbucket.org/mlavin/django-selectable)和 admin tabularInline在其中一個內聯字段中獲得自動完成功能。它適用於在創建時添加的內聯。我遇到的問題是,當用戶向內聯添加另一行時,不會添加自動完成功能。Django可選動態內聯
有一個錯誤並修復此問題在這裏
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);
};
}
}
看起來這應該使自動完成工作與動態添加的行,但我不知道該怎麼做才能工作。 管理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 /管理/媒體/ JS尋找/ inlines.js 它似乎這些觸發器不在django的1.3.1版本中。是否有必要綁定到添加內聯時觸發的事件?這裏有類似的情況 https://bitbucket.org/mlavin/django-selectable/issue/31/dynamically-added-forms 但這是使用formset插件。有沒有辦法使用bindSelectable(行)內聯管理員?
你不應該做任何事情,使這項工作。你正在運行哪個版本? –