Can anyone please send me in the right direction?
I have a django project without frontend so to speak. At the moment all I need is admin page.
My users are able to upload documents, but there is no way for me to show for users who uploaded any given document. I managed to get a dropdown menu where user can choose who uploaded it. But that is not the goal. I want to achieve "uploaded by xxx" where "xxx" is the active (logged in) user.
I fail to find correct keywords for google search and such. Can anyone drop a link to some tutorial/documentation?
I have added this line to my "document" model.
uploaded_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
After I add "uploaded_by" to fieldsets in admin.py I get a dropdown menu where I can choose form all users.
CodePudding user response:
By using django-curentuser library, you can implement this on the model level itself. This means that irrespective of from where the document is uploaded (admin, FormView, API), you will always be able to find the user.
from django_currentuser.db.models import CurrentUserField
# Your model field
uploaded_by = CurrentUserField()
CodePudding user response:
try this:
Document.objects.create(user=request.user)
or:
user_id = request.user.id
Document.objects.create(user=user_id)