Django: Get Current User in Model Save

Django: Get current user in model save

You can tackle this problem from another angle. Instead of changing the models save method you should override the AdminSites save_model method. There you'll have the request object and can access the logged in user data as you already pointed out.

Have a look at this chapter of the docs: Django ModelAdmin documentation save_model

get current user for 'created_by' field in model class

Refer official doc. It explained it pretty well. An example is also there

from django.views.generic.edit import CreateView
from myapp.models import Author

class AuthorCreate(CreateView):
model = Author
fields = ['name']

def form_valid(self, form):
form.instance.created_by = self.request.user
return super().form_valid(form)

https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-editing/#models-and-request-user

get current user to use it in the save method for one of my models

if you have model like this in your models.py:

class Post(models.Model):
title = models.CharField(max_length=100)
created_by = models.ForeignKey(User, editable=False)

Then in your admin.py it should be:

class PostAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
if not change:
obj.created_by = request.user
obj.save()
admin.site.register(Post, PostAdmin);

You can also remove the editable=False if you want to allow the user to assign the created_by to another user.

Send current user when model is saved in Django

 user = models.ForeignKey(settings.AUTH_USER_MODEL)

Your model requires that a user gets set, but obviously it isn't (anymore).

The question is why?

Looking at your code this seens to be the relevant line:

def pre_save(self, obj):
obj.user = self.request.user

Assuming you also upgraded DRF it is quite plausible that this doesn't work anymore: http://www.django-rest-framework.org/topics/3.0-announcement/#changes-to-prepost-save-hooks

So you have to adapt your code and use perform_create as explained in the docs.

How to associate model with current user while saving in django

You can reference the user model directly.

from django.contrib.auth.models import User
from django.db import models

class Post(models.Model):
user = models.ForeginKey(User, on_delete=models.CASCADE)

Or you can use the AUTH_USER_MODEL setting (this is recommended)

from django.conf import settings
from django.db import models

class Post(models.Model):
user = models.ForeginKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

Inside your generic create view you can then associate the request.user with the object inside the form_valid method as explained here Accessing request.user in class based generic view CreateView in order to set FK field in Django

class PostView(CreateView):

def form_valid(self, form):
obj = form.save(commit=False)
obj.user = self.request.user
obj.save()
return http.HttpResponseRedirect(self.get_success_url())

Django: Get current user in model save

You can tackle this problem from another angle. Instead of changing the models save method you should override the AdminSites save_model method. There you'll have the request object and can access the logged in user data as you already pointed out.

Have a look at this chapter of the docs: Django ModelAdmin documentation save_model

How to Save Currently Logged in User to Model in Django

An easy way to get the logged user in one of your views is:

user = request.user

but that is usually when the user model that you have created is something like that in the model.py:

from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
pass

I would suggest using this one to register users because it is automatic and complete.

and from there use the object for what you need. You can pass it on as a context or
source model objects from it.



Related Topics



Leave a reply



Submit