Ad
Change Model Object Url In Django Admin List Using Template Overriding
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.
- Add a button on the right near "Add Category +" which is visible only on the Category List page and takes me to another URL.
- Overriding URLs of Category object so that clicking on each individual category on the list takes to the respective URLs
# 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.
Ad
Answer
First solution:
override list_display_links
and change your field manually
It'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.
Second solution:
Handle change view yourself
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.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad