change list display link in django admin

I believe the correct way of doing it, is subclassing ChangeList and override the url_for_result method to create the correct change url you want. Override the get_changelist in the admin.ModelAdmin subclass to return the new class: from django.contrib.admin.views.main import ChangeList from django.contrib.admin.util import quote class FooChangeList(ChangeList): def url_for_result(self, result): pk = getattr(result, self.pk_attname) return ‘/foos/foo/%d/’ … Read more

Django: accessing the model instance from within ModelAdmin?

I think you might need to approach this in a slightly different way – by modifying the ModelForm, rather than the admin class. Something like this: class OrderForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(OrderForm, self).__init__(*args, **kwargs) self.fields[‘parent_order’].queryset = Order.objects.filter( child_orders__ordernumber__exact=self.instance.pk) class OrderAdmin(admin.ModelAdmin): form = OrderForm

Disable link to edit object in django’s admin (display list only)?

I wanted a Log viewer as a list only. I got it working like this: class LogEntryAdmin(ModelAdmin): actions = None list_display = ( ‘action_time’, ‘user’, ‘content_type’, ‘object_repr’, ‘change_message’) search_fields = [‘=user__username’, ] fieldsets = [ (None, {‘fields’:()}), ] def __init__(self, *args, **kwargs): super(LogEntryAdmin, self).__init__(*args, **kwargs) self.list_display_links = (None, ) It is kind of a mix … Read more

Can “list_display” in a Django ModelAdmin display attributes of ForeignKey fields?

As another option, you can do look ups like: class UserAdmin(admin.ModelAdmin): list_display = (…, ‘get_author’) def get_author(self, obj): return obj.book.author get_author.short_description = ‘Author’ get_author.admin_order_field = ‘book__author’ Since Django 3.2 you can use display() decorator: class UserAdmin(admin.ModelAdmin): list_display = (…, ‘get_author’) @display(ordering=’book__author’, description=’Author’) def get_author(self, obj): return obj.book.author

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)