Reputation: 10840
I would like to display a custom field name (perhaps translated) for a GenericForeignKey
field of a model in Django admin.
Unfortunately, a GenericForeignKey
does not accept verbose_name
(at least in Django 1.6), so how would I actually add a custom name in the admin?
Upvotes: 2
Views: 2059
Reputation: 10840
I was trying to avoid @kirill-kartashov 's method, so I experimented a bit with short_description
attribute.
And I found that, apparently, you can add a short_description
attr. directly to the GenericForeignKey like so:
from django.db import models
from django.utils.translation import ugettext_lazy as _
class SomeModel(models.Model):
content_type = models.ForeignKey(ContentType, null=True)
object_id = models.CharField(max_length=32, null=True)
related_field = generic.GenericForeignKey('content_type', 'object_id',)
related_field.short_description = _('My related field')
Upvotes: 3
Reputation: 141
If you want to show custom name of field in list (not in add/edit form) just try this in admin.py
class YourModelAdmin(admin.ModelAdmin):
list_display = ('field1', 'custom_field')
def custom_field(self, obj):
return obj.real_field
custom_field.short_description = u'Custom name'
Upvotes: 3