title: How do I set value for foreign key field?
body: <p><a href="https://i.stack.imgur.com/LO89W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LO89W.png" alt="enter image description here" /></a></p>
  <p>I want to set the value for this CustomerReg in picture above to request.user.username
  I am able to manually select it, what I want is after login it gets directly assigned to person who has logged in to submit this form.</p>
  <p>views.py(multi_form view and register view)</p>
  <p>I cannot automatically assign CustomerReg field in Customer model.</p>
  <pre><code>def registerPage(request):
      if request.user.is_authenticated:
          return redirect('form')
      else:
          form=CreateUserForm()
  
          if request.method == 'POST':
              form = CreateUserForm(request.POST)
  
              if(form.is_valid()):
                  form.save()
  
                  user=form.cleaned_data['username']
                  messages.success(request, 'Account created for '+ user)
                  return redirect('login')
  
  def multi_form(request):
          form=RegForm()
          if request.method=='POST':
              form=RegForm(request.POST, request.FILES)
              if form.is_valid():
                  form.save()
                  print(request.user.id)
                  messages.success(request, &quot;Your Response has been recorded&quot;)
                  context={'form':form}
                  return render(request, 'customer/index.html', context)
  </code></pre>
  <p>models.py(Customer and CustomerReg model) Customer model has customerReg field as foreign key.</p>
  <pre><code>class CustomerReg(models.Model):
      user=models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
      name=models.CharField(max_length=200, null=True)
      email=models.EmailField(max_length=254)
      
  
  def create_profile(sender, **kwargs):
      if kwargs['created']:
          user_profile=CustomerReg.objects.create(user=kwargs['instance'])
  
  post_save.connect(create_profile, sender=User)
  
  class Customer(models.Model):
  
      id=models.AutoField(primary_key=True, default=None)
      customerReg=models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
      first_name=models.CharField(max_length=200, blank=False, default=None)
      last_name=models.CharField(max_length=200, blank=False, default=None)
  </code></pre>
  <p>forms.py</p>
  <pre><code>class RegForm(ModelForm):
      class Meta:
          model=Customer
          fields=['first_name', 'last_name']
          widgets={
              'first_name':forms.TextInput(attrs={'class': 'form-control', 'placeholder':&quot;First Name&quot;, 'style': 'border-color:hsl(0, 0%, 0%); border-radius: 10px; border-width: 1px;'}),
              'last_name':forms.TextInput(attrs={'class': 'form-control', 'placeholder':&quot;Last Name&quot;, 'style': 'border-color:hsl(0, 0%, 0%); border-radius: 10px; border-width: 1px;'}),
  }
  </code></pre>