A
A
andrew462020-05-31 19:53:46
Django
andrew46, 2020-05-31 19:53:46

How to determine the author of an article in Django?

There is an article model in which the Author field is the user who creates it.

author = models.ForeignKey(User, related_name='author', on_delete=models.CASCADE)

But at the moment it is displayed in the form on the site in the form of a drop-down list with all existing users.
How do I automatically, when a user creates a new article, detect it and save it as the author?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sanya Hihi Haha, 2020-05-31
@ValarMayar

In general, the article model should have an "author" column

if request.user is authenticated:
     Statya.objects.create(title=title,text=text,author= request.user.id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question