Complete ORM core: _inherits, Related write-back, Copy, constraints

Batch 1 (quick-fixes):
- Field.Copy attribute + IsCopyable() method for copy control
- Constraints now run in Write() (was only Create — bug fix)
- Readonly fields silently skipped in Write()
- active_test: auto-filter archived records in Search()

Batch 2 (Related field write-back):
- preprocessRelatedWrites() follows FK chain and writes to target model
- Enables Settings page to edit company name/address/etc.
- Loop protection via _write_related_depth context counter

Batch 3 (_inherits delegation):
- SetupAllInherits() generates Related fields from parent models
- preprocessInheritsCreate() auto-creates parent records on Create
- Declared on res.users, res.company, product.product
- Called in LoadModules before compute setup

Batch 4 (Copy method):
- Recordset.Copy(defaults) with blacklist, IsCopyable check
- M2M re-linking, rec_name "(copy)" suffix
- Replaces simplified copy case in server dispatch

Batch 5 (Onchange compute):
- RunOnchangeComputes() triggers dependent computes on field change
- Virtual record (ID=-1) with client values in cache
- Integrated into onchange RPC handler

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc
2026-04-02 19:57:04 +02:00
parent b57176de2f
commit eb92a2e239
9 changed files with 477 additions and 18 deletions

View File

@@ -429,8 +429,10 @@ func (s *Server) dispatchORM(env *orm.Environment, params CallKWParams) (interfa
if len(params.Args) >= 3 {
if vals, ok := params.Args[1].(map[string]interface{}); ok {
if fieldNames, ok := params.Args[2].([]interface{}); ok {
var changed []string
for _, fn := range fieldNames {
fname, _ := fn.(string)
changed = append(changed, fname)
if handler, exists := model.OnchangeHandlers[fname]; exists {
updates := handler(env, vals)
for k, v := range updates {
@@ -438,6 +440,11 @@ func (s *Server) dispatchORM(env *orm.Environment, params CallKWParams) (interfa
}
}
}
// Run computed fields that depend on changed fields
computed := orm.RunOnchangeComputes(model, env, vals, changed)
for k, v := range computed {
defaults[k] = v
}
}
}
}
@@ -660,23 +667,9 @@ func (s *Server) dispatchORM(env *orm.Environment, params CallKWParams) (interfa
if len(ids) == 0 {
return nil, &RPCError{Code: -32000, Message: "No record to copy"}
}
// Read the original record
records, err := rs.Browse(ids[0]).Read(nil)
if err != nil || len(records) == 0 {
return nil, &RPCError{Code: -32000, Message: "Record not found"}
}
// Remove id and unique fields, create copy
vals := records[0]
delete(vals, "id")
delete(vals, "create_uid")
delete(vals, "write_uid")
delete(vals, "create_date")
delete(vals, "write_date")
// Append "(copy)" to name if it exists
if name, ok := vals["name"].(string); ok {
vals["name"] = name + " (copy)"
}
created, err := rs.Create(vals)
// Parse optional default overrides from args[1]
defaults := parseValuesAt(params.Args, 1)
created, err := rs.Browse(ids[0]).Copy(defaults)
if err != nil {
return nil, &RPCError{Code: -32000, Message: err.Error()}
}