How to make master-detail model / screen in Django?

北慕城南 提交于 2019-12-05 03:27:29

问题


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

The things to discuss are:

  • how to structure code for saving, loading, etc. master and detail models
  • views: files and templates, templates for detail lines, how to add dynamically
  • autocalculated fields (like the total on parent row), where this code goes ?

Edit/Additions:

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

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?


回答1:


  • 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)


来源:https://stackoverflow.com/questions/15646323/how-to-make-master-detail-model-screen-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!