Is it possible to specify a QuerySet model dynamically as a string?

前端 未结 1 629
长情又很酷
长情又很酷 2021-01-03 13:28

I am trying to build a query in Django dynamically. I have a lot of models that I would like to build a query for, but I don\'t want to code the name of the model, I want to

相关标签:
1条回答
  • 2021-01-03 14:13

    Don't try and build querysets via the QuerySet class itself. You should always go via a model's Manager.

    You can get the model via the get_model function defined in django.db.models. It takes parameters of the app name and the model name.

    from django.db.models import get_model
    model = get_model('myapp', 'modelA')
    model.objects.filter(**kwargs)
    
    0 讨论(0)
提交回复
热议问题