28

I'm trying to solve a problem. I have a model hierarchy like this:

class Task(models.Model):
    name = models.CharField(max_length=255)
    number_of_steps = models.IntegerField()

class StepGroup(models.Model):
    task = models.ForeignKey(Task)
    init_date = models.DateField()

class Step(models.Model):
    group = models.ForeignKey(StepGroup)
    name = models.CharField(max_length=255)

I must write a dialog where I create a number of StepGroups. I want the form to be nested because I want to give the user the ability to edit steps that belong to a StepGroup (1-to-many) that belong to a task (1-to-many). So, I want the nested forms to be served in views, instead of in django-admin.

I want to do it with Django inlineformset_factory. However, Django inlineformset_factory only allows one level of nested form. In my case the nesting was 2 levels. Can I achieve it by overriding the BaseInlineFormset? If I can achieve the nested form can I have more depth (3 levels or more) of nested forms?

This is the way I need the form (the number of steps in each group is set by the "number_of_steps" field in the Task model):

+-----------------------------------+
| STEP GROUP 1                      |
|                                   |
| Init date: _____________          |
|                                   |
| Step 1: ________________          |
| Step 2: ________________          |
| Step 3: ________________          |
|                                   |
+-----------------------------------+
| STEP GROUP 2                      |
|                                   |
| Init date: _____________          |
|                                   |
| Step 1: ________________          |
| Step 2: ________________          |
| Step 3: ________________          |
|                                   |
+-----------------------------------+
|                                   |
|             +-------------------+ |
|             | Create step group | |
|             +-------------------+ |
+-----------------------------------+
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
César García Tapia
  • 3,326
  • 4
  • 27
  • 51
  • 1
    My workaround for the same type of problem in the Django admin (dealing with nested relationships) is http://stackoverflow.com/questions/9919780/how-do-i-add-a-link-from-the-django-admin-page-of-one-object-to-the-admin-page-o – agf Jan 05 '14 at 08:01
  • I haven't used this myself, but you could look at https://github.com/nyergler/nested-formset. The readme also includes a link to a couple blog posts that discuss some important considerations (which are good to be aware of before diving into nested formsets) – Studybuffalo Feb 11 '18 at 16:53
  • Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – EJoshuaS - Stand with Ukraine Feb 12 '18 at 02:22
  • you can try this library https://pypi.org/project/django-nested-inline/ you can use in django admin with inline_factory, my comment was deleted by Dharman, i think he don't know what is Django and yours package (libraries) –  Apr 22 '22 at 08:46
  • You don't need any library to so it. Django allow to do IT without additional packages – Maxim Danilov Jul 07 '22 at 15:43

1 Answers1

1

It is possible. I presented it on the Pycon Ru 2021, and start to write articles about it:

https://dev.to/danilovmy/django-nested-inline-redering-4pfg

Shortly:

You can create Formset, One field is this Formset - it is other Formset. Here is a little bit hard plate, how to achieve correct id for every fields. You need also override "is_changed" for field. And the save method.

Biggest problem in Nested - multiuser work. If one user change something, that right now changed other user - you can get complex bug in DB-consistency

Maxim Danilov
  • 2,472
  • 1
  • 3
  • 8