[Answered ]-How to check that an instance detail is being oppened in django admin

1👍

Solution 1

Do you mean when the changeview page has been opened in the admin app? If so you can do this:

class OrderModelAdmin(admin.ModelAdmin)
   def changeform_view(self, request, *args, **kwargs):
       user = request.user()
       # do something with user
       return super().changeform_view(request, *args, **kwargs)

However, is this really what you want to do? Imagine someone accidentally clicks a wrong page. They are automatically assigned. Or maybe someone wants to look at an order without being assigned to it. Quite apart from anything else, this would also go against the principle that a GET request shouldn’t change data.

Solution 2

One alternative would be to override the save_model method in your ModelAdmin:

class OrderModelAdmin(admin.ModelAdmin)
   def save_model(self, request, obj, form, change):
       user = request.user()
       obj.id_of_person = user.id
       return super().changeform_view(self, request, obj, form, change)

This way, whenever someone uses the admin to make a change to an order, that person is then assigned that order.

Other things to consider

The admin app is not designed to be a production ready, customer facing app. The only people using it should really be devs, who understand the data they are changing. The sales department definitely shouldn’t be using the admin app. For this you should write your own views.

Leave a comment