title: Django - Assign Foreign Key upon Form submission
body: <p>Hi I just started learning Django this week and am creating a to do list application, where a user can create multiple to-do lists, each list able to hold multiple items.</p>
  
  <p>Models.py</p>
  
  <pre><code>class Todolist(models.Model):
      listname = models.CharField(max_length=200)
      date = models.DateTimeField(default=datetime.now)
      user = models.ForeignKey(User, default=None, on_delete=models.CASCADE)
  
  class Todo(models.Model):
      todoname = models.CharField(max_length=200)
      date = models.DateTimeField(default=datetime.now)
      user = models.ForeignKey(User, default=None, on_delete=models.CASCADE)
      whichlist = models.ForeignKey(Todolist, on_delete=models.CASCADE, null=True)
  </code></pre>
  
  <p>Views.py</p>
  
  <pre><code>def dolist(request, list_id):
      tdlist = Todolist.objects.get(id=list_id)
      current_user = request.user
      user_todo = Todo.objects.filter(user=current_user, whichlist=tdlist)
      return render(request, 'todolist/tdlists.html', {'tdlist': tdlist, 'todos': user_todo,'list_id':list_id})
  
  def addtodo(request):
      if request.method == 'POST':
          user = request.user
          list_id = request.POST.get('list_id')
          Todo.objects.create(todoname = request.POST['todoname'], user=user, whichlist=list_id)
          return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
      else:
          return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
  </code></pre>
  
  <p>.HTML Template form</p>
  
  <pre><code>&lt;form action="/index/todolist/addtodo" method="POST"&gt;
      {% csrf_token %}
      &lt;input type="hidden" name="list_id" value={{list_id}}&gt;
      &lt;input type="text" name="todoname"/&gt;
      &lt;input type="submit" value="Add to-do item"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Right now, if I take out whichlist parameter in Todo.objects.create(), the to do item is able to be created upon form submission but the foreign key is not assigned so it won't show up on that todo list page. I can go to the Django admin site and manually assign whichlist for the Todo item, and it will then show up on that list page.</p>
  
  <p>How can I make it so that the todo item's foreignkey is automatically assigned? i.e. mario todo item assigned to mario to do list</p>
  
  <p>Any help is appreciated!!</p>