[IMP] fields: infer required=True on related fields

One can infer related=True on a non-stored related field if all fields on the
path are related.  This cannot be done if the related field is stored: when you
create a record, the database row is created first, and the related field is
computed and stored afterwards.  Making the field required in that case would
trigger a non-null constraint violation.
This commit is contained in:
Raphael Collet 2014-10-23 09:46:04 +02:00
parent 0873613767
commit ba1369fbef
1 changed files with 6 additions and 0 deletions

View File

@ -428,10 +428,12 @@ class Field(object):
# determine the chain of fields, and make sure they are all set up
recs = env[self.model_name]
fields = []
for name in self.related:
field = recs._fields[name]
field.setup(env)
recs = recs[name]
fields.append(field)
self.related_field = field
@ -452,6 +454,10 @@ class Field(object):
if not getattr(self, attr):
setattr(self, attr, getattr(field, prop))
# special case for required: check if all fields are required
if not self.store and not self.required:
self.required = all(field.required for field in fields)
def _compute_related(self, records):
""" Compute the related field `self` on `records`. """
# when related_sudo, bypass access rights checks when reading values