10👍
✅
You can’t assign attributes to a property, just like the error message is telling you. You have two options – remove the @property decorator, or provide a wrapper that only the admin uses.
@property
def restaurant_name(self):
return str(self.restaurant)
def restaurant_name_admin(self):
return self.restaurant_name
restaurant_name_admin.admin_order_field = 'restaurant__name'
4👍
Similar with Josh answer but a bit pretty and useful for me because this way also allow you short_description
specifying:
def _restaurant_name(self):
return str(self.restaurant)
_restaurant_name.short_description = ugettext_lazy("restaurant name")
_restaurant_name.admin_order_field = 'restaurant__name'
restaurant_name = property(_restaurant_name)
Now you can use _restaurant_name
as callable in admin changelist_view and restaurant_name
as a property anywhere else.
Source:stackexchange.com