My application displays a foreign field in a TabularInline
form like this:
class RecipeIngredientInlineAdmin(admin.TabularInline):
model = RecipeIngredient
readonly_fields = ['get_unit']
def get_unit(self, obj):
if obj.ingredient:
return obj.ingredient.unit.name
return ''
get_unit.short_description = 'Unit'
fieldsets = [ ( None, { "fields": ["quantity", "get_unit", "ingredient", "position", "group"], },), ]
This works well when displaying a recipe, but if I change/add a new ingredient for a recipe via the TabularInline
form (with unit.name
already defined for this ingredient), get_unit
remains empty, or displays the unit of the previous ingredient. Is it possible to include the unit in the ingredient
label, without overriding the __str__
method for the Ingredient
class globally?
Jan