I have quite some experience with Python and SQL, but am quite new to Django. I’m currently exploring the possibilities of the admin interface. On the admin page of a parent object I have this TabularInline
:
class ScheduleEntryInline(admin.TabularInline):
def __init__(self, day, *args, **kwargs):
self.day = day
super().__init__(*args, **kwargs)
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.filter(day=self.day)
In the parents ModelAdmin
I override get_formsets_with_inlines
to dynamically add multiple of those instances for various days. The forms are displayed as expected and work fine until I try to save my changes.
The day
field of the newly created ScheduleEntry
objects need to be set. My first try was to override save_model
but that seems to be called only for the parent object. Next I found save_related
which is called, but does not yet receive the objects themselves.
It would probably be possible create the objects from the form information passed to save_related
but then I will probably have to handle more save-related code, which will make my code brittle against future changes.
I wonder whether there is a better option to modify the related objects before creating them?