Reputation: 67
Suppose I have a model of Category, and it has been declared in admin.py.
There are two things that I want to do using Django template overriding.
# models.py
class Category(models.Model):
name = models.CharField(max_length=50, null=True, blank=False)
LANGUAGE_ENGLISH = 'en'
LANGUAGE_FRENCH = 'fr'
LANGUAGES = ((LANGUAGE_ENGLISH, 'English'),(LANGUAGE_FRENCH, 'French'),)
language = models.CharField(max_length=12, default=LANGUAGE_ENGLISH, choices=LANGUAGES, blank=False)
created_at = models.DateTimeField(auto_now_add=True)
# admin.py
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'language', 'created_at')
list_filter = ('created_at', 'language')
search_fields = ('name',)
date_hierarchy = 'created_at'
ordering = ['-created_at']
Category in the admin panel
Here, clicking on Lifestyle or Travel should take me to two external urls.
Upvotes: 1
Views: 2041
Reputation: 1145
list_display_links
and change your field manuallyIt's a two steps process.
First, we need to change get_list_display_links
default behavior.
Looking at django's doc and source you will realize it will end up using first item in list_display
.
In your admin class:
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'language', 'created_at')
list_filter = ('created_at', 'language')
list_display_links = [] #< With this, you still can add up a link to your original admin
search_fields = ('name',)
date_hierarchy = 'created_at'
ordering = ['-created_at']
def get_list_display_links(self, request, list_display):
"""
Return a sequence containing the fields to be displayed as links
on the changelist. The list_display parameter is the list of fields
returned by get_list_display().
"""
if self.list_display_links or self.list_display_links is None or not list_display:
# We make sure you still add your admin's links if you explicitly declare `list_display_links`
return self.list_display_links
else:
# We return empty list instead of `list_display[:1]`
# if no `list_display_links` is provided.
return []
Then using this answer, you can make any column customized.
In your admin class:
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
#... same things as you have
def change_view(self, request, object_id, form_url="", extra_context=None):
#Now, you can do pretty much whatever: it's a function based view!
I'd recommend the first one as I believe default admin's change_view
is always usefull.
Upvotes: 2