Is there a possibility to check that an instance detail is being opened in django admin?
An example with orders illustrates it well - imagine that there is a new order instance in a list display of order instances in django admin. (data come from outside django) The order model has the following field which is blank when the instance appears in the list;
ProcessingPersonAdminId = models.IntegerField(verbose_name=_('Processing Person Admin Id'), db_column='ProcessingPersonAdminId', blank=True, null=True)
What I need to do - first person (imagine from sales dep.) that clicks on this particular order instance to view its detail is assigned to it. Before it even displays so the field is already filled in with corresponding user data.
I was thinking about signals but nothing is being saved or updated neither in admin nor in models or anywhere else.
I would appreciate any hint on how to approach this task. Thank you in advance.
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.
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.
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.