Reputation: 55
I am trying to create a model with a foreign key which references the django model User. I have found that you are supposed to use this model when creating something like this:
author = models.ForeignKey(User)
however any time i try to assign this from my view with this line:
if form.is_valid():
c = form.save(commit=False)
c.author=request.user.id
c.save()
I get an error complaining about how author should come from the model User. There is no User property (at least that i (a noob working on their first django project) could find) which has the user id. what is the preferred method for linking a post to it's author in django? am i going about this completely the wrong way? is there a better way of solving this problem that i am just not thinking of?
Upvotes: 0
Views: 1166
Reputation: 51968
For assigning user, it should be like:
c.author= User.objects.filter(id= request.user.id)[0]
Upvotes: 0