Skip to content

Instantly share code, notes, and snippets.

@joeydi
Created March 28, 2024 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeydi/996d66e2a9f2833d12a9220387ba500c to your computer and use it in GitHub Desktop.
Save joeydi/996d66e2a9f2833d12a9220387ba500c to your computer and use it in GitHub Desktop.
Populate inline formset initial values
class ShiftInlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
ordinals = [
'First',
'Second',
'Third',
'Fourth',
'Fifth',
'Sixth',
'Seventh',
'Eighth',
'Ninth',
'Tenth',
]
if not kwargs.get('instance'):
form_index = kwargs.get('prefix').split('-')[1]
if form_index.isdigit():
print(int(form_index))
self.base_fields["name"].initial = ordinals[int(form_index)]
super().__init__(*args, **kwargs)
class ShiftInline(admin.TabularInline):
model = Shift
extra = 0
form = ShiftInlineForm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment