Django 可选择动态内联
我正在使用 django-selectable ( https://bitbucket.org/mlavin/django-selectable ) 和 管理 tabularInline 以获得内联字段之一的自动完成功能。它适用于创建时添加的内联。我遇到的问题是,当用户向内联添加另一行时,不会添加自动完成功能。
这里有一个针对此问题的错误和修复
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/admin/media/js/inlines.js 看来这些触发器不在 django 1.3.1 版本中。是否有必要绑定到添加内联时触发的事件?这里有一个类似的案例 https://bitbucket.org/mlavin/django-selectable/issue /31/动态添加的表单 但这是使用表单集插件。有没有办法在管理内联中使用bindSelectable(row)?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的
jquery.dj.selectable.js
代码用于修补django/contrib/admin/media/js/inlines.js
以调用bindSelectable(row )
当添加新行时。 http://code.djangoproject.com/ticket/15760 已打开,以便此猴子补丁不是必需的,但尚未关闭,并且可能不会在 Django 1.4 中关闭。同样,您不需要做任何事情来使其工作。您不需要更改模板。您不需要编写任何额外的 JS。项目源代码有一个使用动态表格内联的工作示例: https://bitbucket.org/mlavin/django-selectable/src/33e4e93b3fb3/example/core/admin.py#cl-39
The
jquery.dj.selectable.js
code you posted is there to patchdjango/contrib/admin/media/js/inlines.js
to callbindSelectable(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