Make the user in the default model for the current user

I have the following:

from django.contrib.auth.models import User class ClientDetails(models.Model): created_by = models.ForeignKey(User) ... 

How do I make created_by the default for the current logged in user?

(I want to do this so that I can hide it in admin mode, but also because when I save the instance, I don’t want to fill it every time)

+6
python django django-models django-admin
source share
6 answers

Since you need to get the current user in the system from the request object, you cannot get it in the save -method model, but you can, for example, override the admin save_model -method model:

 class MyAdmin(admin.ModelAdmin): def save_model(self, request, instance, form, change): user = request.user instance = form.save(commit=False) if not change or not instance.created_by: instance.created_by = user instance.modified_by = user instance.save() form.save_m2m() return instance 
+12
source share

I had a similar problem recently, this is from the views.py file

 def CircleAdd(request): form = CircleAddForm(request.POST) if form.is_valid(): Circle = form.save(commit=False) Circle.Author = request.user Circle = Circle.save() 

And then I had a form for the circles model, which really was a shell (forms.py)

 class CircleAddForm(ModelForm): class Meta: model = Circle 

Remember to import the form in your submission!

Edit: not even sure if you even need to worry about a separate form, the key bit is a fake commit, followed by the real one

+6
source share

You must override the get_changeform_initial_data method in the model admin class in admin.py as follows:

 # admin.py class ClientDetailsAdmin(admin.ModelAdmin): def get_changeform_initial_data(self, request): get_data = super(ClientDetailsAdmin, self).get_changeform_initial_data(request) get_data['created_by'] = request.user.pk return get_data admin.site.register(ClientDetails, ClientDetailsAdmin) 

Thus, you get the most elegant solution, since the created_by field is registered when creating a new record.

+4
source share

Based on the accepted answer, if you want to do this using class representations, you can follow the instructions in docs by overriding the form_valid() method in the class view:

 # views.py from django.views.generic.edit import CreateView class CreateClient(CreateView): def form_valid(self, form): form.instance.created_by = self.request.user return super(CreateClient, self).form_valid(form) 
+3
source share

Normal modelfields have a default argument. But ForeignKeys is not as much as I know, so I think you need to work with the post_save signal.

+1
source share

I found:

 from django.db import models from django.contrib.auth.models import User class CurrentUserField(models.ForeignKey): def __init__(self, **kwargs): super(CurrentUserField, self).__init__(User, null=True, **kwargs) def contribute_to_class(self, cls, name): super(CurrentUserField, self).contribute_to_class(cls, name) registry = registration.FieldRegistry() registry.add_field(cls, self) class ClientDetails(models.Model): created_by = CurrentUserField() ... 

from here . But is there an easier way?

+1
source share

All Articles