Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>"I would like to add attributes to a Django models programmatically. At class creation time"</p> </blockquote> <p>Don't. "Programmatically" adding columns is silly and confusing. It seems fine to you -- the developer -- who deeply gets the nuances of Django.</p> <p>For us, the maintainers, that code will (a) make no sense and (b) have to be replaced with simple, obvious code that does the same job the simplest most obvious way.</p> <p>Remember, the maintainers are violent sociopaths who know where you live. Pander to them with simple and obvious code.</p> <p>There's no reason to replace a cluster of simple attribute definitions with a tricky-looking loop. There's no improvement and no savings.</p> <ol> <li><p>The run-time performance of the view functions is the same.</p></li> <li><p>The one-time class-definition has saved a few lines of code that are executed once during application start-up. </p></li> <li><p>The development cost (i.e., solving this question) is higher.</p></li> <li><p>The maintenance cost (i.e., turning this code over to someone else to keep it running) is astronomically high. The code will simply be replaced with something simpler and more obvious.</p></li> </ol> <p>Even if you have 100's of currencies, this is still a perfectly bad idea.</p> <blockquote> <p>"I don't want a related model for these prices"</p> </blockquote> <p>Why not? A related model is (1) simple, (2) obvious, (3) standard, (4) extensible. It has almost no measurable cost at run time or development time and certainly no complexity.</p> <blockquote> <p>"For instance, lets say I have a Car model class and want to add one price attribute (database column) per currency, given a list of currencies."</p> </blockquote> <p>That is <em>not</em> a new attribute at all.</p> <p>That is a new value (in a row) of a table that has Car Model and Currency as the keys.</p> <p>You class looks something like this:</p> <pre><code>class Price( models.Model ): car = models.ForeignKey( Car ) currency = models.ForeignKey( Currency ) amount = models.DecimalField() </code></pre> <p>Previous Version of the question</p> <blockquote> <p>"I would like to add attributes to a Django models programmatically, at run time."</p> </blockquote> <p>Don't. There are absolutely no circumstances under which you ever want to add attributes to a database "at run time".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload