@classmethod<EOL><INDENT>def from_model(cls, model, *fields, **named_fields):<DEDENT>
d = ModelDict()<EOL>if not (fields or named_fields):<EOL><INDENT>fields = [f.attname for f in model._meta.concrete_fields]<EOL><DEDENT>not_found = object()<EOL>for name, field in chain(zip(fields, fields), named_fields.items()):<EOL><INDENT>_fields = field.split("<STR_LIT>")<EOL>value = model<EOL>for i, _field in enumerate(_fields, start=<NUM_LIT:1>):<EOL><INDENT>previous_value = value<EOL>value = getattr(previous_value, _field, not_found)<EOL>if value is not_found:<EOL><INDENT>if _field in dir(previous_value):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>".format(<EOL>previous_value, _field<EOL>)<EOL>)<EOL><DEDENT>else:<EOL><INDENT>raise AttributeError(<EOL>"<STR_LIT>".format(<EOL>previous_value, _field<EOL>)<EOL>)<EOL><DEDENT><DEDENT>elif value is None:<EOL><INDENT>if name not in named_fields:<EOL><INDENT>name = "<STR_LIT>".join(_fields[:i])<EOL><DEDENT>break<EOL><DEDENT><DEDENT>d[name] = value<EOL><DEDENT>return d<EOL>
Work-in-progress constructor,
consuming fields and values from django model instance.