• django xadmin多个model的数据渲染在统一个template中


    adminx.py

    demo

    class ModelAdmin(object):
            #....
            
        def get_context(self):
            context = super(SimCardServicesAdmin, self).get_context()
            f = context.get('form',None)
            if f:
                card_id = f['card'].value()
                card_info_obj = SimCardInfo.objects.get(card=card_id)
                card_info_form = SimCardInfoForm(instance=card_info_obj)
           helper = self.get_form_helper()
                layout = Layout(Container(Col('full',
                Fieldset(
                    "", *card_info_form.fields.keys(),
                    css_class="unsort no_title"), horizontal=True, span=12)
                ))
                setattr(helper, 'layout', layout)

                setattr(card_info_form, 'helper',helper) #setattr(card_info_form,
    'helper', self.get_form_helper()) pdb.set_trace() context.update({'form2': card_info_form}) return context

    抽象

    def get_context(self):
        context = super(ModelAdmin, self).get_context()
        original_form = context['form']
        related_form = xxx#related model form object
        context.update({'related_form':related_form})
        return context

    template

    {% crispy form %}
    {% crispy related_form %}
    

      

    注意:

    1. related_form用默认的helper来布局,否则样式不统一。(在get_context中实现)

      help的layout需要自定义,否则栏位默认不显示。

    2. xadmin尽量通过源码来分析问题的解决方案

    转载请注明来源:http://www.cnblogs.com/Tommy-Yu/p/5390555.html

  • 相关阅读:
    Guava入门第四章(Objects)
    Guava入门第三章(Preconditions)
    Guava入门第二章(Splitter)
    Guava入门第一章(Joiner)
    Docker入门第六章
    Docker遇到的问题
    Docker命令图
    2016-08-26-Java比较器的使用
    2017-10-6-MyBatis配置简述
    2017-9-17-Java Exception小结
  • 原文地址:https://www.cnblogs.com/Tommy-Yu/p/5390555.html
Copyright © 2020-2023  润新知