Adapting to a model dynamically django

0

I want to do the following declare a variable that contains the name of the model and travez of those variables execute the queries on that model example model="Person" model.objects.all ()

    
asked by Jefferson Tenecota 19.04.2018 в 18:33
source

1 answer

3

I would recommend you use a dictionary to help you, like this:

modelos = {
    'persona': Persona,
    'animal': Animal,
    # ... otros modelos
}

modelo = 'persona'  # esta sería la llave
modelos[modelo].objects.all()

I hope it's a great help

    
answered by 19.04.2018 / 18:35
source