Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK so the first two answers I got have now been contradicted by others. I've been researching this and I'm going to take a stab at answering it myself. Please vote if you think this is correct and/or comment if you disagree with my reasoning.</p> <ul> <li><p>Methods on models should accept objects and lists of objects, not ids as int/long or lists of id's or anything like that. This is because it will probably be called from a view or form and they have access to full objects from <code>cleaned_data</code> dict. The <code>create()</code> method on manager classes are another example where django itself accepts objects.</p></li> <li><p>The caller of the model layer methods should look up and convert the id's into full objects.</p></li> <li><p>You can override <code>save()</code> but if you do, you should be careful to accept <code>args</code> and <code>**kwargs</code></p></li> <li><p>If the models span applications, you should consider signals instead of overriding <code>save</code></p></li> <li><p>Don't try to get clever overriding the model manager <code>create</code> method. It wont get called if the view layer creates a new object and saves it. If you need to do extra processing before the save, you can override <code>save</code> or <code>__init__</code> or catch a signal. if you override <code>__init__</code> you can check for a pk to determine if it exists in the db yet or not.</p></li> </ul> <p>I'm going to put my create code in a separate method for now until I figure out which of the techniques I like best.</p> <p>I think this is a good set of guidelines for adding methods to the model layer. Anything I missed?</p>
    singulars
    1. This table or related slice is empty.
    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.
    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