How to get the value of a Django Model Field object

前端 未结 3 1772
失恋的感觉
失恋的感觉 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条回答
  •  猫巷女王i
    2021-01-31 10:03

    Assuming you have a model as,

    class SampleModel(models.Model):
        name = models.CharField(max_length=120)

    Then you will get the value of name field of model instance by,

    sample_instance = SampleModel.objects.get(id=1)
    value_of_name = sample_instance.name

提交回复
热议问题