R
R
RokkerRuslan2013-12-21 10:06:52
Django
RokkerRuslan, 2013-12-21 10:06:52

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

3 answer(s)
S
SilentSokolov, 2013-12-21
@RokkerRuslan

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()

A
alrusdi, 2013-12-21
@alrusdi

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/...

A
alternativshik, 2013-12-21
@alternativshik

request.user, like everywhere else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question