Update only ForeignKey field in Django form
<p>I try to update a ForeignKey field of a model in django form. I want a user not to write his name into a input field as an author of the article that he wrote but his <code>id</code>must be saved in the field in <code>Article</code> model. In my html-form I display only a button with hidden input. But in my views.py I write that the form should take <code>user.id</code> and put it in the <code>'article_author'</code> field. </p>

<p>Unfortunately, I've got no updates into table. What am I doing wrong? </p>

<p>my model:</p>

<pre><code>class Article(models.Model):
    article_pub_date = models.DateField(default=None,null=True,verbose_name="Дата публикации")
    article_title = models.CharField(max_length=70, verbose_name="Заголовок", null=True)
    article_text = models.TextField(verbose_name="Текст", null=True)
    article_author = models.ForeignKey(User, verbose_name='Автор', blank=True, null=True)
</code></pre>

<p>my views.py</p>

<pre><code>def article_detail(request, article_id):
    user = request.user
    article = get_object_or_404(Article, pk=article_id)
    author_form = ArticleForm(request.POST or None, instance=article, initial={'article_author': user.id})
    if author_form.is_valid():
        author_form.save()
    else:
        return redirect('/')
</code></pre>

<p>my forms.py</p>

<pre><code>class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = ('article_author',)
        widgets = {'article_author': forms.HiddenInput()}
</code></pre>

<p>and my template:</p>

<pre><code>&lt;form method="POST" action=""&gt;
{% csrf_token %}
{{ author_form.as_p }}
&lt;input type="submit" value="BIND" class="btn"&gt;
&lt;/form&gt;
</code></pre>