title: Forms, Model, and updating with current user
body: <p>I think this works, but I came across a couple of things before getting it to work that I want to understand better, so the question. It also looks like other people do this a variety of ways looking at other answers on stack overflow. What I am trying to avoid is having the user to have to select his username from the pulldown when creating a new search-profile. The search profile model is:</p>
  
  <pre><code>class Search_Profile(models.Model):
      author_of_profile = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=True)
      keyword_string = models.CharField(max_length=200)
      other_stuff = models.CharField(max_length=200)
  </code></pre>
  
  <p>The form I ended up with was:</p>
  
  <pre><code>class Search_Profile_Form(ModelForm):
      class Meta:
          model = Search_Profile
          fields = [ 'keyword_string', 'other_stuff']
  </code></pre>
  
  <p>Where I deliberately left out 'author_of_profile' so that it wouldn't be shown or need to be validated. I tried hiding it, but then the form would not save to the model because a field hadn't been entered.  If I was o.k. with a pulldown I guess I could have left it in.  </p>
  
  <p>I didn't have any issues with the HTML while testing for completeness:</p>
  
  <pre><code>  &lt;form action="" method="POST"&gt;
      {% csrf_token %}
      {{ form.author_of_profile}}
      {{ form.keyword_string }}
      {{ form.other_stuff }}
      &lt;input type="submit" value="Save and Return to Home Page"&gt;
    &lt;/form&gt;
  </code></pre>
  
  <p>And the View is where I ended up treating the form and the model separated, saving the form first, then updating the model, which is where I think there might be some other way people do it.</p>
  
  <pre><code>def New_Profile(request):
      if request.method=='POST':
          form = Search_Profile_Form(request.POST)
          if form.is_valid():
              post=form.save(commit=False)
              # here is where I thought I could update the author of profile  field  somehow with USER
              # but If I include the author_of_profile field in the form it seems I can't.
              post.save()
              #So instead I tried to update the author_of profile directly in the model
              current_profile=Search_Profile.objects.last()
              current_profile.author_of_profile=request.user
              current_profile.save()
              return(redirect('home'))
      else:
          form=Search_Profile_Form()
      return render(request, 'mainapp/New_Profile.html', {'form': form})
  </code></pre>
  
  <p>So a few questions:
  For the Foreign Key in author_of_profile field, is it better to use blank=True, or null=True 
  I ended up using   request.user   rather than  importing from django.contrib.auth.models import User is there any difference?
  My real question though, is the above a reasonable way to get form data and update the database with that data and the user? Or am I missing some other way that is more build in?  </p>