本文介绍了如何在Django中制作主从模型/屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直碰到的一件事是如何使用Django实现主从应用程序。最典型的例子是Invoice和InvoiceLines。

One thing that I allways collide is how to implement a master-detail application with Django. The tipical example is the Invoice and InvoiceLines.

要讨论的事情是:


  • 如何构造代码以保存,加载等主模型和明细模型

  • 视图:文件和模板,明细行模板,如何动态添加

  • 自动计算的字段(如父行中的总数),此代码在哪里?

编辑/添加:

关于自动计算的字段,这是我的第一个解决方案,,在模型的保存方法中会更好吗?

About autocalculated fields, here is my first solution, http://pastebin.com/ZGqNnHuC , would not it be better in the save method of model?

主模型需要详细模型中的值。 (即首先需要计算每个细节的总计,保存每一行,然后求和并保存主文件)与调用calculate()方法相比,它是如何以更Django的方式制作的?

The master model needs values from detail models. (i.e. First it is needed to calculate totals on each detail, save each line, and then sum and save master) How it is made in a more Django way than calling calculate() method?

推荐答案


  • 主模型和详细模型是与。要以相同的形式合并它们,必须使用 Model Formsets (更多)。

  • 动态添加行,您可以考虑

  • 同样适用于自动计算字段,您可以执行用纯JavaScript或通过ajax调用计算视图(因此代码在模板或您的视图中,最终调用模型之类的其他模块,它实际上取决于您执行的计算)

    • master and detail models are two separate models related by a One-to-Many relationship. To incorporate them in the same form you have to use Model Formsets (more details here).
    • to dynamically add rows you may consider this answer
    • the same applies to autocalculate fields, you can do it in pure javascript or with an ajax call to a "calculation view" (thus the code is in the template or in your views, eventually calling other modules like models, it really depends on which calculation are you doing)
    • 这篇关于如何在Django中制作主从模型/屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 16:07