0

I'm creating a Django application based on Django admin app, I spend lot of time to do a task but no success, I wish someone gives me advises to accomplish my work:

I have an admin parent model and two related models admin:

class FirstChildModelAdmin(admin.TabulareInline):
    model = FirstChildModel
    # ...

class SecondChildModelAdmin(admin.TabulareInline):
    model = SecondChildModel
    # ...
class ParentModelAdmin(admin.ModelAdmin):
    model = ParentModel
    inlines = [FirstChildModelAdmin, SecondChildModelAdmin]
    # ...

In the admin change_view, if I click on Save and contiue editing button I need to generate multiple inline forms for the SecondChildModel (by overriding InlineModelAdmin.get_extra method)

So, in my admin.py I tried to create my custom formset for the SecondChildModelAdmin, as explained here Admin inline forms initial data for every instance, but did not reach what I want.

This my custom formset:

class CustomModelInlineFormset(BaseInlineFormSet):
    model = SecondChildModel

def __init__(self, *args, **kwargs):
    super(CustomModelInlineFormset, self).__init__(*args, **kwargs)
    print("custom formset")
    if 'files' in kwargs:
        # Here some code to retrieve files content

        self.initial = [{'fiedl_one': 'some_content', 'fiedl_one': '...'}, {....}]

This is my question:

How can I generate multiple inline forms of the SecondChildModel and populate his fields from content files selected on FirstChildModel? Is it possible to do that?

I will appreciate any helps

Thanks

djvg
  • 11,722
  • 5
  • 72
  • 103
MeBex
  • 488
  • 1
  • 5
  • 20

0 Answers0