Updating data in the API

I’m new to python, kindly i need help. I’m trying to update in an API, first am query to get the row i want update, i don’t want to change all the attributes in the row just some. I have tried to use setattr() it is setting the new attributes with values but it is trying to replace as the results am running into integrity error. Below is my code.
`
def update_town(session, town_dict):
town = session.query(Town).where(Town.id == town_dict[“id”] ).one()
#setting new values

for key, value in town_dict.items():
    town_attribute = setattr(town, key, value)
    
    town_attribute = value
    
    print(town_attribute)
session.commit() 


return 'ok'

`

below is my route
`
@app.route(’/town’, methods = [‘POST’, ‘GET’, ‘PATCH’, ‘PUT’,‘DELETE’])

defining the function without pasing arguments

def town():
if request.method == ‘POST’:
addTown(session, request.json)
return ‘ok’

if request.method == 'GET':
    return getTown(session, request.json)

if request.method == 'DELETE':
    return delete_town(session, request.json)

if request.method == 'PUT':
    return update_town(session, request.json)

`

Hey David,

Just a suggestion, but you may want to edit your post to make it easier for people to read your code without too much effort. In my experience (which is in no way limited to this forum) I’ve found that the easier it is for others to read what one has posted, the better the chances of getting some feedback.

You understand how markdown works, right? If not I can post a link to a guide.

I hope you don’t think that I’m being patronising (I can assure you that is not my intention), it’s just that your post is not easy to read, as is.

Peace.

I see , no thank you …I understand… I’ll try to do so

Hi,

You need three backticks for the “code fence” when you post, you only have one.

```
code goes here
```

A single backtick ` is only useful for inline code, like this. For multiple lines, you need to use three.

Unfortunately there is not enough information to answer your question.

At the minimum, we will need to know which API or framework you are using, and what error you get. Then you need to hope there is somebody here who knows that API and can answer your question.