Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I agree that its hard to do template editing so, I create a custom widget to show an <code>anchor</code> on the admin change view page(can be used on both forms and inline forms).</p> <p>So, I used the anchor widget, along with form overriding to get the link on the page.</p> <p><strong>forms.py:</strong></p> <pre><code>class AnchorWidget(forms.Widget): def _format_value(self,value): if self.is_localized: return formats.localize_input(value) return value def render(self, name, value, attrs=None): if not value: value = u'' text = unicode("") if self.attrs.has_key('text'): text = self.attrs.pop('text') final_attrs = self.build_attrs(attrs,name=name) return mark_safe(u"&lt;a %s&gt;%s&lt;/a&gt;" %(flatatt(final_attrs),unicode(text))) class PostAdminForm(forms.ModelForm): ....... def __init__(self,*args,**kwargs): super(PostAdminForm, self).__init__(*args, **kwargs) instance = kwargs.get('instance',None) if instance.blog: href = reverse("admin:appname_Blog_change",args=(instance.blog)) self.fields["link"] = forms.CharField(label="View Blog",required=False,widget=AnchorWidget(attrs={'text':'go to blog','href':href})) class BlogAdminForm(forms.ModelForm): ....... link = forms..CharField(label="View Post",required=False,widget=AnchorWidget(attrs={'text':'go to post'})) def __init__(self,*args,**kwargs): super(BlogAdminForm, self).__init__(*args, **kwargs) instance = kwargs.get('instance',None) href = "" if instance: posts = Post.objects.filter(blog=instance.pk) for idx,post in enumerate(posts): href = reverse("admin:appname_Post_change",args=(post["id"])) self.fields["link_%s" % idx] = forms..CharField(label=Post["name"],required=False,widget=AnchorWidget(attrs={'text':post["desc"],'href':href})) </code></pre> <p>now in your <code>ModelAdmin</code> override the <code>form</code> attribute and you should get the desired result. I assumed you have a <code>OneToOne</code> relationship between these tables, If you have one to many then the <code>BlogAdmin</code> side will not work. </p> <p><strong>update:</strong> I've made some changes to dynamically add links and that also solves the <code>OneToMany</code> issue with the <code>Blog</code> to <code>Post</code> hope this solves the issue. :)</p> <p><strong>After Pastebin:</strong> In Your <code>PostAdmin</code> I noticed <code>blog_link</code>, that means your trying to show the <code>blog</code> link on <code>changelist_view</code> which lists all the posts. If I'm correct then you should add a method to show the link on the page.</p> <pre><code>class PostAdmin(admin.ModelAdmin): model = Post inlines = [SubPostInline, DefinitionInline] list_display = ('__unicode__', 'enabled', 'blog_on_site') def blog_on_site(self, obj): href = reverse("admin:appname_Blog_change",args=(obj.blog)) return mark_safe(u"&lt;a href='%s'&gt;%s&lt;/a&gt;" %(href,obj.desc)) blog_on_site.allow_tags = True blog_on_site.short_description = 'Blog' </code></pre> <p>As far as the showing <code>post</code> links on <code>BlogAdmin</code> <code>changelist_view</code> you can do the same as above. My earlier solution will show you the link one level lower at the <code>change_view</code> page where you can edit each instance.</p> <p>If you want the <code>BlogAdmin</code> page to show the links to the <code>post</code> in the <code>change_view</code> page then you will have to include each in the <code>fieldsets</code> dynamically by overriding the <code>get_form</code> method for <code>class BlogAdmin</code> and adding the link's dynamically, in <code>get_form</code> set the <code>self.fieldsets</code>, but first don't use tuples to for <code>fieldsets</code> instead use a list. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload