Access Django model's fields using a string instead of dot syntax?

前端 未结 4 1445
青春惊慌失措
青春惊慌失措 2020-12-13 05:49

In Django, I can do this:

test = Test.objects.get(id=1)
test.name

I want to be able to access the properties using dynamically generated st

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 05:58

    Other answers still stand, but now you can do this using Django's _meta.get_field().

    test._meta.get_field('name')
    

    Note that the Model _meta API has begun its official support and documentation as of 1.8, but has been distributed and used in prior versions before its official documentation.

提交回复
热议问题