Here's the python code to produce an admin related widget with the edit and delete link also.
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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Hi! Thanks for your work! I updated it for Django 1.4 and made an application:
github: django-relatedwidget
#
Please login first before commenting.