5

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?

jordiburgos
  • 5,964
  • 4
  • 46
  • 80

1 Answers1

5
  • 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)
Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
furins
  • 4,979
  • 1
  • 39
  • 57
  • If possible, the links need to be updated, it goes to Django 1.4 which does not exist – diek Jul 04 '17 at 11:39