Reputation: 4008
I want to autocomplete 2 fields:
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='created_by')
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='updated_by')
for normal users and for django admin.
If for normal users I can use get request.user from my view(found some solutions here on the site),but this is not the case for admin/staff because I don't control the views, so I'm searching for a solution at the Model level by overwriting the save function.
Upvotes: 0
Views: 1625
Reputation:
May be the solution with default user by Middleware help you.
django-populate-user-id-when-saving-a-model
Upvotes: 1
Reputation:
from django.contrib.auth.models import User
created_by = models.ForeignKey(User, related_name='created_by')
updated_by = models.ForeignKey(User, related_name='updated_by')
Then in your view, you can do this :
form.created_by = request.user
form.updated_by = request.user
It's going to autocomplete by the current user who made the action.
May be I didn't understant your question, so may be this is what you're looking for : How to auto insert the current user when creating an object in django admin?
Upvotes: 1
Reputation:
is pretty simple, just add to your field: editable=False Like this:
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, editable=False, related_name='created_by')
Upvotes: 0