How to get the value of a Django Model Field object

前端 未结 3 1767
失恋的感觉
失恋的感觉 2021-01-31 09:28

I got a model field object using field_object = MyModel._meta.get_field(field_name). How can I get the value (content) of the field object?

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 10:01

    If you want to access it somewhere outside the model You can get it after making an object the Model. Using like this

    OUSIDE THE MODEL CLAA:

    myModal = MyModel.objects.all()
    
    print(myModel.field_object)
    

    USING INSIDE MODEL CLASS
    If you're using it inside class you can simply get it like this

    print(self.field_object)
    

提交回复
热议问题