Background
You have to create a class by inheriting ModelAdmin
class and state the model associated with that ModelAdmin
. This is ModelAdmin
basics and you can find it in the documentation. Also Inspect View basics can be found in this documentation.
class OrderAdmin(ModelAdmin):
model = Order
inspect_view_enabled = True
inspect_view_fields = (
'id',
'date',
)
I have this model called Order
and I need to display only id
and date
in the wagtail inspect view. Also I have an Address
model with a one-to-one relation to the Order
model. I need to show city
from that Address
in the inspect view.
Answer
But if just add new field like following, it will give an error as wagtail
can't find a field called city
in Order
model.
inspect_view_fields = (
'id',
'date',
'city',
)
How wagtail resolve fields
For every field in inspect_view_fields
array, wagtail calls a method called get_dict_for_field
to get the associated label and the value for that field. You can easily override this method. After overriding you should tell wagtail to used the overridden method. You can do so as follows.
class OrderInspectView(InspectView):
def get_dict_for_field(self, field_name): # Override to support custom fields
# Do the implementation for custom fields
return super().get_dict_for_field(field_name)
class OrderAdmin(ModelAdmin):
model = Order
inspect_view_enabled = True
inspect_view_class = OrderInspectView # Tell wagtail to use our class instead of the default class
inspect_view_fields = (
'id',
'name',
'city', # Custom field
)
What you need to do now is to get the Address
associated with the Order
and return it. You can get the current inspecting Order
using self.instance
.
def get_dict_for_field(self, field_name):
if field_name == 'city':
qs = Address.objects.filter(order=self.instance.id)
value = str(qs[0].city) if len(qs) > 0 else '-'
return {
'label': 'City',
'value': value,
}
return super().get_dict_for_field(field_name)