60👍
The solution by Masatsugu Hosoi works fine, but you can also use the decorator and set short_description on the property getter (fget):
@property
def my_property(self):
return u'Returns some calculations'
my_property.fget.short_description = u'Property X'
14👍
I’ve ended up adding the field to the ModelAdmin
class as well, redirecting to the model:
class MyAdmin(admin.ModelAdmin):
list_display=['my_property',]
def my_property(self, object):
return object.my_property
my_property.short_description = _("Property X")
- [Django]-Django 'objects.filter()' with list?
- [Django]-How to run celery as a daemon in production?
- [Django]-Get javascript variable's value in Django url template tag
5👍
def _my_property(self):
return u'Returns some calculations'
_my_property.short_description = 'Property X'
my_property = property(_my_property)
- [Django]-Django 2.0 path error ?: (2_0.W001) has a route that contains '(?P<', begins with a '^', or ends with a '$'
- [Django]-What is a Django "app" supposed to mean?
- [Django]-How to use refresh token to obtain new access token on django-oauth-toolkit?
3👍
I don’t know how to do this with the @ decorator syntax, but you can do:
def my_property(self):
return u'Returns some calculations'
property_x = property(my_property)
Then, in your admin class set:
list_display = ['whatever', 'something_else', 'property_x']
The capitalization isn’t perfect, but it will display ‘Property x’. It may be possible to get Django to display the __doc__
for a property so you could control the formatting better, but it’s a start.
You can add the doc= keyword argument as such:
property_x = property(my_property, doc='Property X')
- [Django]-Exception: You cannot access body after reading from request's data stream
- [Django]-Remove duplicates in a Django query
- [Django]-Get all related Django model objects
1👍
in admin.py into your ModelAdmin add this code :
def my_property(self , obj):
return 'Returns some calculations'
my_property.short_description = 'Property X'
after that add my_property to list_display
list_display=['my_property',]
like this :
class MyAdmin(admin.ModelAdmin):
list_display=['my_property',]
def my_property(self , obj):
return 'Returns some calculations'
my_property.short_description = 'Property X'
- [Django]-What is the difference render() and redirect() in Django?
- [Django]-Tailwindcss: fixed/sticky footer on the bottom
- [Django]-Django Admin: how to display properties defined on model in an inline?
0👍
You can redefine the property class as New-style class.
So you can add all attributes you need
class property(property):
"""Redefine property object"""
pass
@property
def my_property(self):
return u'Returns some calculations'
my_property.short_description = _("Property X")
Actually I’m not sure it is a good practice. Anyway I don’t see any contraindication.
- [Django]-Django-cors-headers not work
- [Django]-How do I set a default, max and min value for an integerfield Django?
- [Django]-How to pass an array in Django to a template and use it with JavaScript
-1👍
This thread is old, but in case someone wanders down here, the solution that works is to just add a short_description
on the method/property
in your class.
def my_property(self):
# Some things done
my_property.short_description = 'Property'
This will solve the problem.
- [Django]-Django query datetime for objects older than 5 hours
- [Django]-Test Django views that require login using RequestFactory
- [Django]-Disable a method in a ViewSet, django-rest-framework