【问题标题】:Django Admin: How to dynamically insert values into the add inline formDjango Admin:如何将值动态插入到添加内联表单中
【发布时间】:2009-11-06 04:21:37
【问题描述】:

假设我有这个模型:

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 

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

【问题讨论】:

    标签: django forms inline admin add


    【解决方案1】:

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

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

    如果您想即时更改货币,则将表单子类化并覆盖 init,您可以在 self.fields['currency'] 上施展魔法。

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

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

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

    【讨论】:

    • 感谢您的回答,但您似乎误解了我的问题。我已经修改了我的问题,所以它更清楚。
    【解决方案2】:

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

    完成后,我认为您的管理问题可以这样解决:

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

    您将在哪里使用自定义 ReadOnlyWidget,例如 the one here

    【讨论】:

      【解决方案3】:

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

      {% 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'
      

      【讨论】:

        猜你喜欢
        • 2011-07-30
        • 2010-10-05
        • 2016-11-10
        • 2020-01-22
        • 1970-01-01
        • 2011-07-11
        • 1970-01-01
        • 2014-02-05
        • 1970-01-01
        相关资源
        最近更新 更多