Admin related widget wrapper with edit / delete link (python widget)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from django.conf import settings
from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

class RelatedFieldWidgetWrapper(RelatedFieldWidgetWrapper):
    
    class Media:
        js = ("%swidgets/admin/js/related-widget-wrapper.js" % settings.STATIC_URL,)
    
    def __init__(self, *args, **kwargs):
        self.can_change_related = kwargs.pop('can_change_related', None)
        self.can_delete_related = kwargs.pop('can_delete_related', None)
        super(RelatedFieldWidgetWrapper, self).__init__(*args, **kwargs)
    
    @classmethod
    def from_contrib_wrapper(cls, wrapper, can_change_related, can_delete_related):
        return cls(wrapper.widget, wrapper.rel, wrapper.admin_site,
                   can_add_related=wrapper.can_add_related,
                   can_change_related=can_change_related,
                   can_delete_related=can_delete_related)
    
    def get_related_url(self, rel_to, info, action, args=[]):
        return reverse("admin:%s_%s_%s" % (info + (action,)), current_app=self.admin_site.name, args=args)
    
    def render(self, name, value, attrs={}, *args, **kwargs):
        rel_to = self.rel.to
        info = (rel_to._meta.app_label, rel_to._meta.object_name.lower())
        self.widget.choices = self.choices
        attrs['class'] = ' '.join((attrs.get('class', ''), 'related-widget-wrapper'))
        context = {'widget': self.widget.render(name, value, attrs, *args, **kwargs),
                   'name': name,
                   'media_prefix': settings.ADMIN_MEDIA_PREFIX,
                   'can_change_related': self.can_change_related,
                   'can_add_related': self.can_add_related,
                   'can_delete_related': self.can_delete_related}
        if self.can_change_related:
            if value:
                context['change_url'] = self.get_related_url(rel_to, info, 'change', [value])
            template = self.get_related_url(rel_to, info, 'change', ['%s'])
            context.update({
                            'change_url_template': template,
                            'change_help_text': _('Change related model')
                            })
        if self.can_add_related:
            context.update({
                            'add_url': self.get_related_url(rel_to, info, 'add'),
                            'add_help_text': _('Add Another')
                            })
        if self.can_delete_related:
            if value:
                context['delete_url'] = self.get_related_url(rel_to, info, 'delete', [value])
            template = self.get_related_url(rel_to, info, 'delete', ['%s'])
            context.update({
                            'delete_url_template': template,
                            'delete_help_text': _('Delete related model')
                            })
        
        return mark_safe(render_to_string('widgets/admin/related-widget-wrapper.html', context))

More like this

  1. Admin related widget wrapper with edit / delete link (widget js) by nasp 2 years, 6 months ago
  2. Admin related widget wrapper with edit / delete link (python model admin) by nasp 2 years, 6 months ago
  3. Admin related widget wrapper with edit / delete link (html) by nasp 2 years, 6 months ago
  4. View all log entries in the admin 2 by tgandor 4 months ago
  5. models.py with django_dag models for parts hierarchy by j_syk 2 years, 8 months ago

Comments

benjaoming (on June 14, 2012):

Hi! Thanks for your work! I updated it for Django 1.4 and made an application:

github: django-relatedwidget

#

(Forgotten your password?)