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

220
Vistas
django pass field-name as variable in get_or_create

I am trying to see if I can pass field name as a variable in get_or_create (since I have a function where the key in the kwargs can vary)

Like so:

def convert_value(cell_value, field_to_lookup):
        rem_obj, created = Rem.objects.get_or_create(field_to_lookup=cell_value)
        print ('created? ',created)
        return rem_obj

The above wont work since it would look for 'field_to_lookup' as the key.

This post suggests using getattr but not sure if that'll be applicable in this case since I will again need to assign the output to a variable

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

0

This post helped. Now passing the field-value pair as dict which allows passing variables for field names. Here's the code:

def convert_value(cell_value, field_to_lookup):
        rem_obj, created = Rem.objects.get_or_create(**{field_to_lookup:cell_value})
        print ('created? ',created)
        return rem_obj

Alternatively, I could directly just pass the dict to the function.

about 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