Django AttributeError: 'MyModel' object has no attribute 'username'
<p>I have a model, <code>MyModel</code> that is linked via <em>ForeignKey</em> to Django's <code>User</code> model. MyModel was initially linked using <code>models.OneToOneField</code> but I later changed it to <code>models.ForeignKey</code> and also deleted some fields.</p>

<p><strong>Initial Models.py:</strong></p>

<pre><code>from django.contrib.auth.models import User
from myapp.models import mymodel

class MyModel(models.Model):
    user = models.OneToOneField(User, blank=True, null=True, related_name='mymodel')
    username = models.CharField(max_length=145, blank=True, null=True)
    name = models.CharField(max_length=45, blank=True, null=True)
</code></pre>

<p><strong>Editted Model.py:</strong>   </p>

<pre><code>class MyModel(models.Model):
    user = models.ForeignKey(User, blank=True, null=True, related_name='mymodel')
    name = models.CharField(max_length=45, blank=True, null=True)
</code></pre>

<p>I deleted <em>migration folder</em> and my <strong>DB</strong> and recreated them anew but each time I try retrieving data from MyModel via <code>MyModel.objects.all()</code> or <code>User</code> object I get <strong>AttributeError: 'MyModel' object has no attribute 'username'</strong></p>

<p>Your updated model doesn't have an attribute <code>username</code>, because the field name is <code>user</code>.</p>

python django