title: Associating Objects and Users in Django using Generic Views
body: <p>I'm attempting to associate a collection of Objects with a logged in user.</p>
  
  <blockquote>
    <p>Urls.py</p>
  </blockquote>
  
  <pre><code>urlpatterns = [
  path('new/collection', views.CollectionCreate.as_view(), name="create"),
  path('edit/collection/&lt;int:pk&gt;', views.CollectionUpdate.as_view(), name='update'),
  path('delete/collection/&lt;int:pk&gt;', views.CollectionDelete.as_view(), name='delete'),
  ]
  </code></pre>
  
  <blockquote>
    <p>Views.py</p>
  </blockquote>
  
  <p>This page generates a form where a logged in <code>User</code> enters the title of the <code>Collection</code>, and creates it. As it is right now, the <code>Collection</code> does not get associated with any user. I know that this is the point in which I need to attach the created <code>Collection</code> to the logged in <code>User</code> but I have reached a stop point.</p>
  
  <pre><code>class CollectionCreate(CreateView):
      model = Collection
      template_name = 'upload/collection_form.html'
      fields = [
          'title'
      ]
  </code></pre>
  
  <blockquote>
    <p>Models.py</p>
  </blockquote>
  
  <p>In this class, a Collection is being associated with a default Django User.</p>
  
  <pre><code>class Collection(models.Model):
      user = models.OneToOneField(User, blank=True, null=True, on_delete=models.CASCADE)
      title = models.CharField(max_length=140)
      tags = models.CharField(max_length=140)
      upload_date = models.DateTimeField(auto_now_add=True)
  
      def get_absolute_url(self):
          return reverse('gallery:detail', kwargs={'pk': self.pk})
  
      def __str__(self):
          return self.title + ' by ' + self.author
  
      class Meta:
          ordering = ['-upload_date']
  </code></pre>
  
  <p>Given a <code>form.ModelForm</code>, I know I can access the passed data by using <code>form = ModelForm(request.POST)</code> inside a <code>def post(self, ...)</code> function.</p>
  
  <p>I don't know how to do that in a CreateView.</p>