from itertools import groupby
init_data = {'wood': {'color': 'red', group: 'old'}, 'stone': {'color': 'gray', group: 'old'}, 'glass': {'color': 'white', group: 'new'}}
grouped_data = {
k: list(
{
_k: _v for d in (
{'material': av[0]},
av[1]
) for _k,_v in d.items()
} for av in v
) for k, v in groupby(
init_data.items(),
lambda x: x[1]['group']
)
}
legal = validated_data.get('legal')
instance.legal.legal_audit = legal.get('legal_audit')
instance.legal.taxation_ico = legal.get('taxation_ico')
instance.legal.security_regulation = legal.get('security_regulation')
instance.legal.company_setup = legal.get('company_setup')
instance.legal.save()