Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
How to edit formset that has multiple forms inside related via generic foreign to the model of the current edit view?

In my recent project I had to create a generic model with a single file field, because the idea was to use multiple file uploads across multiple system models.

For this I created this generic model with generic foreign key and in the add view I create a formset with the extra = 1 field and in the template via jquery I add forms inside my formset.

In addition it works quietly, but I'm not able to adjust my edit view.

In my edit view I try it:

ModelGenericFormset = modelformset_factory(
        ModelsFileGeneric,
        form=AddModelsGenericForm
    )
query_files = ModelsFileGeneric.objects.filter(
        object_id=model_related_to_generic_model_with_file_field.id,
    )
files = ModelGenericFormset(queryset=query_files)

and in request.post:

files_form_set = ModelGenericFormset(
            request.POST,
            request.FILES
        )
for file_form in files_form_set:
                file = file_form.save(commit=False)
                setattr(
                    'upload_destination',
                    'path_to_upload'
                )
                file.content_object = model_related_to_generic_model_with_file_field
                file.save()

An observation: As this model where the file field is is an extremely generic model (for this reason the use of the generic foreign key), I also need at runtime (in the save in view after the post) to change the upload_to attribute of the field (this already Was fixed and works ok). I make this edition of the "upload_to" attribute because depending on the model to which I am sending multiple files, it will vary the path from where the files will be saved.

But in save of the edit view this error occurs:

The ModelsFileGeneric could not be changed because the data didn't validate.

And the error is in:

file = file_form.save(commit=False)

I don't know what to do anymore. Thanks!

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda