I am new to django and am stuck at a very basic level. I want to create a model that stores the article posted by the the user. In the model I am able to save my article but how can I save the user id in that model. On submitting this form only the title and body are saved.
My models.py file is as follows:
class Article(models.Model):
Title = models.CharField(max_length=169)
Body = models.TextField()
Author_id = models.CharField(max_length=3)
def __unicode__(self):
return self.Title
My forms.py is as follows:
class AddArticle(forms.ModelForm):
class Meta:
model = Article
fields = ('Title', 'Body')
and my views.py is:
def UploadArticle(request):
if request.POST:
form = AddArticle(request.POST)
if form.is_valid():
Article.Author_id = request.user.id #I guess my mistake lies here, but how to avoid it
form.save()
return HttpResponseRedirect('/articles/all')
else:
form = AddArticle()
args = {}
args.update(csrf(request))
args['form']=AddArticle()
return render_to_response('add.html', args)
Any help please