Code

Opened 7 years ago

Closed 6 years ago

#3015 closed enhancement (worksforme)

[patch] Small change to django.contrib.admin.urls to allow permalink() usage

Reported by: Collin Grady <cgrady@…> Owned by: nobody
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

Description

The current urls.py for the admin app doesn't name some of the params, so trying to use permalink() with it fails - as this is useful in some situations, and doesn't harm the function of the existing app at all, I think it should be updated so it does work with it.

Attachments (1)

admin-urls-permalink-compatible.patch (1.8 KB) - added by Collin Grady <cgrady@…> 7 years ago.

Download all attachments as: .zip

Change History (7)

Changed 7 years ago by Collin Grady <cgrady@…>

comment:1 Changed 7 years ago by Collin Grady <cgrady@…>

Afterthought, as an example you can do something like this with this change applied:

    def get_admin_url(self):
        return ('django.contrib.admin.views.main.change_stage', None, {'app_label': 'blog', 'model_name': 'entry', 'object_id': self.id})
    get_admin_url = permalink(get_admin_url)

It might be possible to grab that info through _meta or such also, but I haven't played with that much :)

comment:2 Changed 7 years ago by Collin Grady <cgrady@…>

Hmm, might be possible to make this a generalized and always-there function on models?

    def get_admin_url(self):
        return ('django.contrib.admin.views.main.change_stage', None, {'app_label': self._meta.app_label, 'model_name': self._meta.module_name, 'object_id': self.__getattribute__(self._meta.pk.attname)})

That seems to pick up app/model/id values instantly - or would it need a check to see if there even is a pk?

comment:3 Changed 7 years ago by Collin Grady <cgrady@…>

Didn't realize this before, but the last entry in the dict could be changed to

'object_id': getattr(self, self._meta.pk.attname)

comment:4 Changed 7 years ago by Simon G. <dev@…>

  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 7 years ago by Collin Grady <cgrady@…>

What do I need to improve in the patch? :)

comment:6 Changed 6 years ago by SmileyChris

  • Resolution set to worksforme
  • Status changed from new to closed

Permalink doesn't fail, you just need to pass the arguments positionally at the moment:

    @models.permalink
    def get_admin_url(self):
        return 'django.contrib.admin.views.main.change_stage', [
            self._meta.app_label, self._meta.module_name,
            getattr(self, self._meta.pk.attname)
        ]

(and changing it probably isn't worth it with newforms-admin on the way)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.