Re: Admin Change List: Display Foreign Keys as Links to Related Change Form
This solution did not work out of the box but it helped me figure out
a simple but tedious solution:
In models.py:
-----------------
class AppParent(models.Model):
...
def get_url(self):
return
urlresolvers.reverse('admin:myapp_appparent_change',args=(self.id,))
class AppChild
app_parent = models.ForeignKey(AppParent)
...
def edit_parent(self):
return mark_safe(u'<a href="%s">%s</a>' %
(self.app_parent.get_url(),self.app_parent))
Then in admin.py:
----------------------
from manager.models import AppChild
class AppChildAdmin(admin.ModelAdmin):
list_display = ('id','edit_parent',...)
The case is very important above. reverse() requires the model_name
parameter to be the parent model class name converted to all lower
case, with no underscores between words. That took a lot of
experimentation to figure out. Where is that documented?
This whole hack should not be needed: any database web GUI should
automatically display foreign keys as hyperlinks to allow drilling
into parent records. Is this being added to Django Admin Change List I
hope?
Any ideas on how to genericize this solution so I don't have to update
every model/change_list to display foreign keys as hyperlinks?
Thanks for all help.
Lee
On Nov 13, 8:10 pm, Sumii L <leesum...@gmail.com> wrote:
> Perhaps you have a foreignkey, Assumption is that person
> just like this:( in your models)
> def go_person(self):
> return mark_safe(u'<a href="%s">%s</a>' %
> (self.person.get_absoulte_url(), label))
> go_person.allow_tags = True
> go_person.short_description = 'GO_PERSON'
>
> --and in your admin.py:
> list_dispaly = ('go_person', 'other attributes....')
>
> I hope this is what you want!
>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home