How to convert nested OrderedDict to Object
<p>I have dictionary like this</p>

<pre><code>data = OrderedDict([('name', 'NewIsland'), ('Residents', [OrderedDict([('name', 'paul'), ('age', '23')])])])
</code></pre>

<p>and I want to convert it to class object.</p>

<p>Here are my django models.</p>

<pre><code>class Country(models.Model):
    name = models.CharField(max_length=20)

class Residents(models.Model):
    name = models.CharField(max_length=20)
    age = models.PositiveSmallIntegerField()
    country = models.ForeignKey('Country', related_name='residents')
</code></pre>

<p>When I code like this</p>

<pre><code>result = Country(**data)
</code></pre>

<p>I got exception </p>

<blockquote>
  <p>'residents' is an invalid keyword argument for this function</p>
</blockquote>

<p>How can I convert it to class object that I can access residents with result.residents[idx]?</p>

<p>You can do it by manually creating child objects</p>

<pre><code>residents_data = data.pop('Residents')    
result = Country(**data)
for rdata in residents_data:
    result.residents.add(Residents(**rdata), bulk=False)
</code></pre>

python django