|
|
|
|
|
by khorpy
3643 days ago
|
|
When considering using ORM we need to answer simple questions: does it help to decrease code size? In most cases ORM code is same size as SQL query. Secondly, does it hide complexity? No, it just adds up one more layer that in fact increases complexity and makes it harder to debug. Finally, does it protect us from errors or impoves code quality? This is rarely the case. Instead of using ORM I prefer moving data retrieval code to database layer with help of views. There is also updatable views that we can use to simplify database updates on code side and sometimes avoid using transactions. Separation of code and data logic is great concern when deciding to implement ORM. Nowdays database are very smart and convinient so there is no need to use ORM. |
|
myobject, created = MyObject.objects.get_or_create(field1='1' , field2=field2)
That would take a number of lines. One query to check if the object exists, another to create it / retrieve it, plus application code to deal with that SQL.