Answer the question
In order to leave comments, you need to log in
How to define user in Django admin?
Suppose there is a plate with orders, you need to know who entered this order into the database, and if I use the admin panel as this, then how to determine the user who logged in?
Answer the question
In order to leave comments, you need to log in
I didn't quite understand what you want. You say that it works through the admin, but you do not inherit from admin.ModelAdmin, and you do not have custom views ... do you have a standard admin or ...?
Here is an example code for a standard admin panel:
class Order(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
creator = models.ForeignKey('User', null=True, blank=True)
class OrderAdmin(admin.ModelAdmin):
fieldsets = [....]
def save_model(self, request, obj, form, change):
if getattr(obj, 'creator', None) is None:
obj.creator = request.user
obj.save()
Override the save_model method of your successor from admin.ModelAdmin - request is available there and, accordingly, request.user https://docs.djangoproject.com/en/dev/ref/contrib/...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question