title: Setting user_id when saving in view - Django
body: <p>I need to set the user that is creating the post in the add view:</p>
  
  <pre><code>@login_required 
  def add(request):
      if request.method == 'POST':
          form = BusinessForm(request.POST)
          form.user_id = request.user.id
          if form.is_valid():
              form.save()
              return HttpResponseRedirect(reverse('listing.views.detail'), args=(f.id))
      else:
          form = BusinessForm()
      return render_to_response('business/add.html', {'form':form},
          context_instance=RequestContext(request))
  </code></pre>
  
  <p>I assign the user id <code>form.user_id = request.user.id</code> but when saving, it still gives me an error <code>Column user_id cannot be null</code></p>
  
  <p>Am I doing something wrong? Thanks</p>
  
  <p>EDIT:</p>
  
  <p>I am excluding the user from the form in the model:</p>
  
  <pre><code>class BusinessForm(ModelForm):
      class Meta:
          model = Business
          exclude = ('user',)
  </code></pre>
  
  <p>Could that be causing the problem?? How can I work around this?</p>
  
  <p>EDIT 2:</p>
  
  <p>Edited my BusinessForm() class as suggested but did not work:</p>
  
  <pre><code>class BusinessForm(ModelForm):
      class Meta:
          model = Business
          exclude = ('user',)
      def __init__(self, *args, **kwargs):
          self.request = kwargs.pop('request', None)
          return super(BusinessForm, self).__init__(*args, **kwargs)
  
      def save(self, *args, **kwargs):
          kwargs['commit']=False
          obj = super(BusinessForm, self).save(*args, **kwargs)
          if self.request:
              obj.user = self.request.user
          obj.save()
  </code></pre>
  
  <p>Business model</p>
  
  <pre><code>class Business(models.Model):
      name = models.CharField(max_length=200)
      user = models.ForeignKey(User, unique=False)
      description = models.TextField()
      category = models.ForeignKey(Category)
      address = models.CharField(max_length=200)
      phone_number = models.CharField(max_length=10)
      website = models.URLField()
      image = models.ImageField(upload_to='business_pictures',blank=True)
  </code></pre>