11
I had similar problem and I came up with custom widget plus some tweaks to model form. Here is the widget:
from django.utils.safestring import mark_safe
class ModelLinkWidget(forms.Widget):
def __init__(self, obj, attrs=None):
self.object = obj
super(ModelLinkWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
if self.object.pk:
return mark_safe(
u'<a target="_blank" href="../../../%s/%s/%s/">%s</a>' %\
(
self.object._meta.app_label,
self.object._meta.object_name.lower(),
self.object.pk, self.object
)
)
else:
return mark_safe(u'')
Now since widget for each inline need to get different object in constructor you canβt just set it in standard way, but in Formβs init method:
class TheForm(forms.ModelForm):
...
# required=False is essential cause we don't
# render input tag so there will be no value submitted.
link = forms.CharField(label='link', required=False)
def __init__(self, *args, **kwargs):
super(TheForm, self).__init__(*args, **kwargs)
# instance is always available, it just does or doesn't have pk.
self.fields['link'].widget = ModelLinkWidget(self.instance)
- [Django]-How do you know if memcached is doing anything?
- [Django]-Docker image error: "/bin/sh: 1: [python,: not found"
- [Django]-Determine variable type within django template
52
I did something like the following in my admin.py:
from django.utils.html import format_html
from django.core.urlresolvers import reverse
class MyModelInline(admin.TabularInline):
model = MyModel
def admin_link(self, instance):
url = reverse('admin:%s_%s_change' % (instance._meta.app_label,
instance._meta.module_name),
args=(instance.id,))
return format_html(u'<a href="{}">Edit</a>', url)
# β¦ or if you want to include other fields:
return format_html(u'<a href="{}">Edit: {}</a>', url, instance.title)
readonly_fields = ('admin_link',)
- [Django]-How to make two django projects share the same database
- [Django]-FileUploadParser doesn't get the file name
- [Django]-Example of Django Class-Based DeleteView
31
The currently accepted solution here is good work, but itβs out of date.
Since Django 1.3, there is a built-in property called show_change_link = True
that addresses this issue.
This can be added to any StackedInline or TabularInline object. For example:
class ContactListInline(admin.TabularInline):
model = ContactList
fields = ('name', 'description', 'total_contacts',)
readonly_fields = ('name', 'description', 'total_contacts',)
show_change_link = True
The result will be something line this:
- [Django]-Limit number of characters with Django Template filter
- [Django]-Django class-based view: How do I pass additional parameters to the as_view method?
- [Django]-Django FileField: How to return filename only (in template)
- [Django]-How can I serialize a queryset from an unrelated model as a nested serializer?
- [Django]-Elegant setup of Python logging in Django
- [Django]-Django template can't loop defaultdict
- [Django]-.filter() vs .get() for single object? (Django)
- [Django]-How do I print out the contents of my settings in a django shell?
- [Django]-How do you perform Django database migrations when using Docker-Compose?
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-Django Background Task
- [Django]-Select distinct values from a table field
Source:stackexchange.com