Django is failing to save data related to User table. It says it doesn't have `mymodel_set` attribute
<p>I have a model linked to Django <code>User</code> model but when I try saving to that model using <code>User</code> instance, it says <strong>'User' object has no attribute 'mymodel_set'</strong></p>

<p><strong>My models.py:</strong></p>

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

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

<p><strong>My views.py:</strong></p>

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

def register(request):
    #gets data here from template
    user = User(username=reg_username, password=reg_password)
    user.save()
    user.mymodel_set.create(name= display_name)
    return HttpResponse('Success')
</code></pre>

<p>If the related object existed, you would use <code>mymodel</code>, but it does not exist and the relationship is void, so it cannot be accessed via the <code>user</code>. Create it first and set the relationship to that user:</p>

<pre><code>mymodel = MyModel.objects.create(name=display_name, user=user)
#                                                   ^^^^ set related user
</code></pre>

<p>The <code>_set</code> suffix is usually used for reverse <code>ForeignKey</code> relationships and not for <code>OneToOne</code> relationships.</p>

<p>Also note that the <code>related_name</code> on the user field was already specified as <code>mymodel</code>, and the related field can now be accessed from the <code>User</code> model via  <code>user.mymodel</code></p>

python django