Separating Quick Create From Typical Create in Odoo 15
Published: 2023-01-23 01:17:47

Odoo supports `quick create` option to related fields of type `Many2one`. Such capability let the user create an instant record based on the record name. A case may arise where you may need to differentiate between `quick create` & the `typical create` in the backend create method. The easy way to get the backend aware of such create method is through the `context` parameter. In the context parameter you could pass such information that the call is from the quick create. However you need to the update the `BasicModel` object of the web client. It is located in the method `_applyX2OneChange`. As the method full of promises calls. The only way to update it, is to override it. The method definition could be found there:
https://github.com/odoo/odoo/blob/3fdc00f74338fc9eed913771ff28ec447650a0db/addons/web/static/src/legacy/js/views/basic/basic_model.js#L1642
so you could scaffold a new odoo module & create a new javascript file then add it to the manifest definition. your javascript file would be as following. please note line 18 where I updated the context:
```javascript
1 odoo.define("product_seq.BasicModel", function (require) {
2     "use strict";
3 
4     var BasicModel = require("web.BasicModel");
5 
6     BasicModel.include({
7         _applyX2OneChange: async function (record, fieldName, data, options) {
8             options = options || {};
9             var self = this;
10              if (!data || (!data.id && !data.display_name)) {
11                 record._changes[fieldName] = false;
12                 return Promise.resolve();
13              }
14              const field = record.fields[fieldName];
15              const coModel = field.type === "reference" ? data.model : field.relation;
16              const allowedTypes = ["many2one", "reference"];
17              if (allowedTypes.includes(field.type) && !data.id && data.display_name) {
18                  let _context = this._getContext(record, {
19                      fieldName: fieldName,
20                      viewType: options.viewType,
21                      additionalContext: {'quick_create': true},
22                 }) 
23                 const result = await this._rpc({
24                     model: coModel,
25                     method: "name_create",
26                     args: [data.display_name],
27                     context: _context,
28                });
29                 if (!result) {
30                     record._changes[fieldName] = false;
31                     return Promise.resolve();
32                 }
33                 data = { id: result[0], display_name: result[1] };
34             }
35             var relatedID;
36             if (record._changes && fieldName in record._changes) {
37                 relatedID = record._changes[fieldName];
38             } else {
39                 relatedID = record.data[fieldName];
40             }
41             var relatedRecord = this.localData[relatedID];
42             if (relatedRecord && data.id === this.localData[relatedID].res_id) {
43                 return Promise.resolve();
44             }
45             var rel_data = _.pick(data, "id", "display_name");
46             const viewType = options.viewType || record.viewType;
47             const fieldInfo = record.fieldsInfo[viewType][fieldName] || {};
48             const fieldOptions = fieldInfo.options || {};
49             var def;
50             if (
51                 rel_data.display_name === undefined ||
52                 fieldOptions.always_reload
53             ) {
54                 def = this._rpc({
55                     model: coModel,
56                     method: "name_get",
57                     args: [data.id],
58                     context: this._getContext(record, { fieldName, viewType }),
59                 }).then(function (result) {
60                     rel_data.display_name = result[0][1];
61                 });
62             }
63             return Promise.resolve(def).then(function () {
64                 var rec = self._makeDataPoint({
65                     context: record.context,
66                     data: rel_data,
67                     fields: {},
68                     fieldsInfo: {},
69                     modelName: coModel,
70                     parentID: record.id,
71                 });
72                 record._changes[fieldName] = rec.id;
73             });
74         },
75     });
76 });
```