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

153
Vistas
Django - Access field by iterating over field list

I am storing all many to many fields on an object using this -

m2m_fields = list(model_source_obj._meta.many_to_many)

I want to iterate over this list to make changes to another object of the same type. I have written the code for it as such -

for field in m2m_fields:
    model_target_obj.field.add(**something)

However, I keep getting the error model_target_obj class type has no attribute 'field'. How can I fix this?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You're not accessing the field, you're accessing the (non-existent) literal attribute called "field".

You could use getattr:

obj = getattr(model_target_obj, field)
obj.add(**something)

But I suspect that there are better ways to do whatever you are trying to do.

over 4 years ago · Santiago Trujillo Denunciar
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