# Access DB Object's column using a string

**URL:** <https://forum.djangoproject.com/t/access-db-objects-column-using-a-string/10256>\
**Category:** Using Django\
**Created:** [October 26, 2021, 3:50pm UTC](https://forum.djangoproject.com/t/access-db-objects-column-using-a-string/10256 "2021-10-26T15:50:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarknessNacho](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/darknessnacho/32/5503_2.png) [@DarknessNacho](https://forum.djangoproject.com/u/DarknessNacho)\
**Post date:** [October 26, 2021, 3:50pm UTC](https://forum.djangoproject.com/t/access-db-objects-column-using-a-string/10256/1 "2021-10-26T15:50:26Z")

</div>

How can I access my DB object’s column?

Normally you would do something like this to access a column inside a database:

```auto
if poststr != None:
            dbvalue = Prijzen.objects.get(id=1)

            dbvalue.datumOphaling = value
            dbvalue.save()

            return HttpResponse(value)

```

(datumOphaling is a column inside my database)

But I want to be able to acces the column like this:

```auto
if poststr != None:
            dbvalue = Prijzen.objects.get(id=1)
            poststr = request.POST.get('functie', None)
            dbvalue.poststr = value
            dbvalue.save()

            return HttpResponse(value)

```

How would I achieve this, because in my last example it is not working?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 26, 2021, 4:51pm UTC](https://forum.djangoproject.com/t/access-db-objects-column-using-a-string/10256/2 "2021-10-26T16:51:33Z")

</div>

Standard reminder - This is Python. You access a field within a model by variable name the same way you access any attribute from any class - using the [`getattr`](https://docs.python.org/3/library/functions.html#getattr) and [`setattr`](https://docs.python.org/3/library/functions.html#setattr) functions.

In this case, it would be something like this:  
`setattr(dbvalue, poststr, value)`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 27, 2021, 12:00pm UTC](https://forum.djangoproject.com/t/access-db-objects-column-using-a-string/10256/3 "2021-10-27T12:00:44Z")

</div>

By the way, unless you strictly filter that post data, you’re creating a massive security vulnerability by doing this. Unless there’s significantly more to this method than you’re showing here, this is a **really** bad design.
