Created
March 28, 2024 13:48
-
-
Save joeydi/996d66e2a9f2833d12a9220387ba500c to your computer and use it in GitHub Desktop.
Populate inline formset initial values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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