2

I'd like to understand if there are simple(DRY) ways to display inline formsets straight from django forms in a template with a simple view. Using floppyform or other 3rd party apps is accepted.

Let's say we have 2 (or more) simple models, such as:

class ModelA(models.Model):
    field_a_one = models.CharField()
    field_a_two = models.CharField()


class ModelB(models.Model):
    field_rel_a = models.ForeignKey(ModelA)
    field_b_one = models.CharField()

Instead of a OneToMany relation we could have a ManyToMany relation.

what I'd like, in its simplest form, is to be able to do in my template:

{{ model_a_form }}

and get something like:

field_a_one [  input  ]
field_a_two [  input  ]

    field_b_one [   input   ]
    [Delete]

    [ add one more ] 

...similarly to what happens in the admin.

I would then like, in the view, to only have to do:

class ModelAView(UpdateView):

   model = ModelA

and have a ModelAForm to customize behaviour eg. which fields to include, which order, etc. - as usual with django Forms - but including the inline relationships, and their customization (eg. default html templates such as tabular or not, etc.). I guess something very similar to what the admin does.

The closest q/a I found on SO is Creating form using Generic_inlineformset_factory from the Model Form but that does not provide an all-in-one solution.

There's also this: http://haineault.com/blog/155/ blog, but again you have to provision the formsets in the views yourself. This just does not seem "DRY" to me! Any better solution?

On more related question, which points to that blog, but comments are not very positive about the approach: django class-based views with inline model-form or formset

Community
  • 1
  • 1
Stefano
  • 18,083
  • 13
  • 64
  • 79

0 Answers0