2

I am trying to display different fields inside a class inheriting StackedInline depending on whether it is a new or an existing instance (i.e. add or change form). I know that inside a class inheriting ModelAdmin I can override the get_form method to achieve this as described here. Looking at the django docs it doesn't say that get_form is a shared feature under InlineModelAdmin options. So is there a way for me to display different fields for adding and changing in an inline? I am a novice. Any help is greatly appreciated.

Community
  • 1
  • 1
robert
  • 141
  • 6

1 Answers1

2

Because those work with formsets. Override get_formset instead.

jpic
  • 32,891
  • 5
  • 112
  • 113
  • Does clicking _add_ call `get_formset`. I tried logging inside `get_formset` by it only logs for existing inlines when the page is loaded. Any thoughts on how I can change fields displayed on _add_? – robert Jul 03 '13 at 14:07
  • Inspect the inline formset HTML. You will see that a hidden form of the formset has prefix="__prefix__", it is cloned and the prefix is replaced when clicking *add*. `get_formset` does not need to be called again. To change fields for existing objects, you should alter the ModelForm instances in `formset.forms` which have `.instance.pk` before returning the formset. – jpic Jul 03 '13 at 15:55
  • You wouldn't happen to have some pseudo code example or code example just lying around would you? Please... – robert Jul 04 '13 at 06:48