Django Admin:如何动态地将值插入添加内联表单

时间:2009-11-06 04:21:37

标签: django forms inline admin add

假设我有这个模型:

class Foo(models.Model):
    bar = models.ForeignKey(Bar)
    currency = models.ForeignKey(Currency) # currency is just an example
    is_active = models.BooleanField()

现在假设Foo是Bar的内联。我总是希望指出每种货币的价值?如果我可以用一个小部件替换这些货币下拉菜单,该小部件只返回带有隐藏字段的文本名称。 例如,在添加页面上,而不是让内联显示:

 - currency drop down menu    is_active checkbox 
 - currency drop down menu    is_active checkbox 
 - currency drop down menu    is_active checkbox 

让它显示出来:

 - currency name 1    is_active checkbox 
 - currency name 2    is_active checkbox 
 - currency name 3    is_active checkbox 
 - currency name 4    is_active checkbox 
 - currency name 5    is_active checkbox 

实现这一目标的正确方法是什么?我假设我必须覆盖一些表单类方法。感谢。

3 个答案:

答案 0 :(得分:0)

如果您总是想使用相同的货币:

currency = models.ForeignKey(Currency, default = lambda: Currency.objects.get(...)

如果您想更改表格中的货币子类并覆盖 init ,您可以在self.fields['currency']上执行您的魔术。

如果您想隐藏该字段,请在表单类的该字段上使用widget = forms.HiddenInput()

我认为这可以回答你的问题,但不是你真正的问题。使用[django-currency] [1]灵活处理货币。

[1]:http://code.google.com/p/django-currencies/ django-currency

答案 1 :(得分:0)

这里要解决的第一个问题是为每个Bar预先生成适当的链接Foos。您可以在Bar上使用自定义save()方法,或使用信号,或在BarManager上的Bar工厂方法中执行此操作...

一旦完成,我认为你的管理问题可以这样解决:

class FooInline(admin.TabularInline):
    formfield_overrides = {models.ModelChoiceField: {'widget': ReadOnlyWidget}}
    model = Foo
    extra = 0

您使用自定义ReadOnlyWidget,例如the one here

答案 2 :(得分:0)

我用javascript解决了它。 将下面插入表格或堆叠模板的顶部。它假定name列名为name。

{% with inline_admin_formset.opts as i %}
    {% if i.pre_filled %}
        <script language="JavaScript" type="text/javascript">
        jQuery(function($) {    
            var pre_values = [
            {% for name in i.pre_filled %}
                {% if forloop.last %}
                [{{ name.id }}, "{{ name.name }}"] 
                {% else %}
                [{{ name.id }}, "{{ name.name }}"],    
                {% endif %}
            {% endfor %}
            ];

            $.each( pre_values,
                function( i, value ){
                    $('div.inline-group div.tabular').each(function() {
                        $("#id_{{ i.verbose_name|lower|cut:" " }}_set-" + i + "-{{ i.pre_field }}").after(value[1]);
                        $("#id_{{ i.verbose_name|lower|cut:" " }}_set-" + i + "-{{ i.pre_field }}").val(value[0]).hide();
                        $("#lookup_id_{{ i.verbose_name|lower|cut:" " }}_set-" + i + "-{{ i.pre_field }}").hide();    
                        $("strong").hide();
                    });
                }
            );
        }); 
        </script>
    {% endif %}
{% endwith %}

这就在你的inline.py中:

class MyCustomInline(admin.TabularInline):
    pre_filled = []
    pre_field = ''

    def get_fieldsets(self, request, obj=None): 
        if not obj and self.pre_filled and self.pre_field:
            count = self.pre_filled.count()
            self.extra = count
            self.max_num = count
            if self.raw_id_fields:
                self.raw_id_fields.append(self.pre_field)
            else:
                self.raw_id_fields = [self.pre_field]

        return super(MyCustomInline, self).get_fieldsets(request, obj)

class FooInline(MyCustomInline):
    model = Foo
    pre_filled = Currency.objects.all()
    pre_field = 'currency'
相关问题