Finding Admin URLs for Custom Django Model

How to search admin URL for custom model?

If I know the model, I can get the url by doing something like:

>>> print urlresolvers.reverse('admin:myapp_mymodel_change', args=(obj.id,)) /admin/myapp/mymodel/123/ 

I have a common foreign key on the model, and I would like to provide a link in admin to the corresponding page for changing the object. Since this can be any type of model, I cannot easily use reverse (). Is there a way that I could just do this as follows?

 >>> get_admin_change_url(obj) /admin/myapp/mymodel/123/ 
+4
source share
1 answer

Once you have the object, you can access its label and application name in the _meta class, and then dynamically create the name of the admin admin URL.

 app_label = obj._meta.app_label model = obj._meta.module_name reverse('admin:%s_%s_change' % (app_label, model), args=(obj.id,)) 
+5
source

Source: https://habr.com/ru/post/1379374/


All Articles